Codeforces 653B B. Bear and compressing (DFS)

Source: Internet
Author: User

Topic Links:

B. Bear and compressing

Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak are so young and he knows only first six letters of the Chinese alphabet: 'a ', 'B ' , 'c ', 'd ', 'e ' and 'f '.

You is given a set Of  Q  possible operations. Limak can perform them in any order, any operation could be applied any number of times. The  i -th operation is described by a string  a i  of length II and a String  b Span class= "Lower-index" > i  of length one. No of  Q  possible operations have the same String  a i .

When Limak have a string  s  he can perform the  i -th operation On  s  if the first Letters Of  s  match a two-letter string  a i . Performing The  i -th operation removes first and letters Of  s  and inserts there a string  b i . See the Notes sections for further clarification.

You may note this performing an operation decreases, the length of a string s exactly by 1. Also, for some sets of operations there is a string that cannot is compressed any further, because the first and Lette RS don ' t match any ai.

Limak wants to start with a string of length n and perform n -1 operations to finally get a O Ne-letter string "a". In what many ways can he choose the starting string to being able to get "a"? Remember that Limak can use only letters he knows.

Input

The first line contains integers n and q (2≤ n ≤6, 1≤ q ≤36)- The length of the initial string and the number of available operations.

The nextQLines describe the possible operations. The i-th of them contains II strings ai and bi (| Ai| = 2, | bi| = 1). It ' s guaranteed thataiaJ for ij< /c26> and that all ai and bI consist for only first six low Ercase 中文版 Letters.

Output

Print the number of strings of length n that Limak would be is able to transform to string 'a ' by applying on Ly operations given in the input.

Examplesinput
3 5
AB A
CC C
CA A
EE c
FF D
Output
4
Input
2 8
AF E
DC D
CC F
BC b
Da b
EB a
BB b
FF C
Output
1
Input
6 2
BB A
BA A
Output
0
Note

In the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There is 4 such strings: "ABB", "cab", "CCA", "EEA". The first one Limak can compress using Operation 1 times (changing "ab" to a "a"). The first operation would change 'ABB ' to ' AB ' and thesecond operation would change 'ab ' to 'a '.

Other three strings is compressed as follows:

    • "cab" "AB" "a"
    • "CCA" "CA" "a"
    • "EEA" "CA" "a"

In the second sample, the only correct initial string was "EB" because it can be immediately compressed to "a".

Test instructions: To give a pair of string, the first two characters match the first 2 characters can be removed to the string after a character, ask the last to get a length of n string has how many;

Idea: Looking forward from a, the reverse operation;

AC Code:

#include <bits/stdc++.h>using namespacestd;inta[8],n,q,ans=0;Chars[ +][7];intDfsCharXintnum) {    if(num>=n-1) {ans+=a[x-'a'];return 0;}  for(intI=0; i<q;i++)    {       if(s[i][4]==x) {Chary=s[i][1]; DFS (Y,num+1); }    }}intMain () {scanf ("%d%d",&n,&q);  for(intI=0; i<q;i++) {scanf ("%s", s[i]+1); scanf ("%s", s[i]+4); a[s[i][4]-'a']++; } DFS ('a',1); cout<<ans<<Endl; return 0;}

Codeforces 653B B. Bear and compressing (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.