Codeforces 653B Bear and Compressing【DFS】
2024-09-02 22:37:18
题目链接:
http://codeforces.com/problemset/problem/653/B
题意:
要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a。已知第i个操作将字符串中开头的字符串ai换成字符bi,长度减少1。问一共有多少种字符串构造方法。
分析:
直接dfs一下就好啦~~~
代码:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 105;
char a[maxn][2], b[maxn];
int n,q;
int dfs(int pos, int cnt)
{
if(cnt <= 0) return 1;
int ans = 0;
for(int i= 0; i < q; i++){
if(b[i] == a[pos][0]){
ans += dfs(i, cnt - 1);
}
}
return ans;
}
int main (void)
{
cin>>n>>q;
int pos;
long long ans = 0;
vector<int>v;
for(int i = 0; i < q; i++){
cin>>a[i][0]>>a[i][1]>>b[i];
if(b[i] == 'a') v.push_back(i);
}
for(int i = 0; i < v.size(); i++){
ans += dfs(v[i], n - 2);
}
cout<<ans<<endl;
}
最新文章
- IO多路复用之poll总结
- tyvj1106 登山
- drdb
- 1106c语言语法树
- Table of Contents - 设计模式
- android开发3:四大基本组件的介绍与生命周期
- 朱丽叶—Cuda+OSG
- [题解]bzoj 3223 文艺平衡树
- mysql数据库表结构与表约束
- ZooKeeper 分布式共享锁的实现
- 小甲鱼OD学习第6讲
- 弹出层之2:JQuery.BlockUI
- npm 安装卸载模块
- JAVA中字符串操作几种方式对比
- kali 解决Metasploit拿到shell后显示中文乱码问题
- 【Java虚拟机】浅谈Java虚拟机
- 746. Min Cost Climbing Stairs
- Shiro的使用
- ImageView的android:scaleType各属性含义(zz)
- tomcat服务器开启gzip功能的方法