Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak is so young that he knows only first six letters of the English alphabet: 'a', 'b', 'c', 'd', 'e' and 'f'.
You are given a set of q possible operations. Limak can perform them in any order, any operation may be applied any number of times. Thei-th operation is described by a stringai of length two and a stringbi of length one. No two ofq possible operations have the same stringai.
When Limak has a string s he can perform thei-th operation ons if the first two letters ofs match a two-letter stringai. Performing thei-th operation removes first two letters ofs and inserts there a string bi. See the notes section for further clarification.
You may note that performing an operation decreases the length of a string s exactly by 1. Also, for some sets of operations there may be a string that cannot be compressed any further, because the first two letters don't match anyai.
Limak wants to start with a string of length n and performn - 1 operations to finally get a one-letter string "a". In how many ways can he choose the starting string to be able to get "a"? Remember that Limak can use only letters he knows.
The first line contains two integers n andq (2 ≤ n ≤ 6,1 ≤ q ≤ 36) — the length of the initial string and the number of available operations.
The next q lines describe the possible operations. Thei-th of them contains two stringsai andbi (|ai| = 2, |bi| = 1). It's guaranteed that ai ≠ aj fori ≠ j and that all ai andbi consist of only first six lowercase English letters.
Print the number of strings of length n that Limak will be able to transform to string "a" by applying only operations given in the input.
3 5 ab a cc c ca a ee c ff d
4
2 8 af e dc d cc f bc b da b eb a bb b ff c
1
6 2 bb a ba a
0
In the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There are4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation1 two times (changing "ab" to a single "a"). The first operation would change "abb" to "ab" and the second operation would change "ab" to "a".
Other three strings may be compressed as follows:
- "cab"
"ab"
"a"
- "cca"
"ca"
"a"
- "eea"
"ca"
"a"
In the second sample, the only correct initial string is "eb" because it can be immediately compressed to "a".
题意:给出q组数据,每组数据有一个长度为2的字符串a和一个字符bi,表示字符串a可以转化成bi,现在限定一段长度为n的字符串,当它最前的两个字符和前面的数据可以匹配,则可以转换,转换次数不限。现在问有多少段这样的字符串最终可以转化成'a'。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <stack>
#include <map>
using namespace std;
char s[] = {'a', 'b', 'c', 'd', 'e', 'f'}, a[10];
map<string, char> mp;
int n, q, sum;
bool check(){
stack<char> ss;
for(int i = n - 1;i >= 0;i--) ss.push(a[i]);
while(!ss.empty()){
if(ss.size()==1){
if(ss.top() == 'a'){
//puts(a);
sum++;
return 1;
}
else return 0;
}
char a = ss.top();
ss.pop();
char b = ss.top();
ss.pop();
string v;
v += a;
v += b;
map<string, char>::iterator it = mp.find(v);
if(it == mp.end()) return 0;
ss.push(it->second);
}
}
bool dfs(int num){
if(num == n){
a[num] = '\0';
//puts(a);
if(check()) return 1;
return 0;
}
for(int i = 0;i < 6;i++){
a[num] = s[i];
dfs(num+1);
}
return 0;
}
int main()
{
int i;
char a[5], b[5];
scanf("%d %d", &n, &q);
for(i = 0;i < q;i++){
scanf("%s %s", a, b);
mp.insert(pair<string, char>(a, b[0]));
}
dfs(0);
printf("%d\n", sum);
}