您的位置:首页 > 编程语言 > Go语言

Codeforces 653B Bear and Compressing

2016-04-01 18:13 441 查看
题目链接:http://codeforces.com/problemset/problem/653/B

题        意:给一对string,前面两个字符匹配的话可以吧前边2个字符去掉换成这对string后面一个字符,问最后能得到a的长度为n的字符串有多少个;

思        路:从a往前找,反过来操作,直接比较a匹配后所形成的字符串的第一个字母,若有符合条件的操作,就替换,直到字符串的长度大于或等于n判断是否符合条件。

代码如下:#include <stdio.h>
#include <iostream>
#include <cstring>
#include <sstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;

string a[40], b[40];
LL ans;
int n, q;

void dfs( string ss )
{
if( ss.size() == n ) {
ans++;
return ;
}
for( int i = 1; i <= q; i ++ )
{
if( ss[0] == b[i][0] )//从一边开始加字母
dfs( a[i]+ss.substr(1) );//去掉b的值,加上a的值
}
}

int main()
{
while( scanf ( "%d %d", &n, &q ) != EOF )
{
for( int i = 1; i <= q; i ++ )
{
cin>>a[i]>>b[i];
}
ans = 0;
dfs( "a" );
printf("%lld\n", ans );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm codeforces