codeforces 653B B. Bear and Compressing(dfs)

题目链接:

B. Bear and Compressing

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 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. The i-th operation is described by a string ai of length two and a string bi of length one. No two of q possible operations have the same string ai.

When Limak has a string s he can perform the i-th operation on s if the first two letters of s match a two-letter string ai. Performing the i-th operation removes first two letters of s 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 any ai.

Limak wants to start with a string of length n and perform n - 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.

Input

The first line contains two integers n and q (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. The i-th of them contains two strings ai and bi (|ai| = 2, |bi| = 1). It‘s guaranteed thatai ≠ aj for i ≠ j and that all ai and bi consist of only first six lowercase English letters.

Output

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.

Examples

input

3 5ab acc cca aee cff d

output

4

input

2 8af edc dcc fbc bda beb abb bff c

output

1

input

6 2bb aba 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 are 4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation 1 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".

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

思路:从a往前找,反过来操作;

AC代码:

#include <bits/stdc++.h>
using namespace std;
int a[8],n,q,ans=0;
char s[40][7];
int dfs(char x,int num)
{
    if(num>=n-1){ans+=a[x-‘a‘];return 0;}
    for(int i=0;i<q;i++)
    {
       if(s[i][4]==x)
       {
           char y=s[i][1];
            dfs(y,num+1);
       }
    }
}
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=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;
}
时间: 2024-08-01 22:43:01

codeforces 653B B. Bear and Compressing(dfs)的相关文章

cf B. Bear and Compressing

B. Bear and Compressing 思路:字符串是什么样的有多种情况不定,但我们总是知道最后一个肯定是a,那么我们就可以反着推,看a是由那个序列变过来的,拿案例一的数据说,比如我们先看见ca,可以变成a,那a的上一个状态就是ca,由“ca”代替“a“,此时长度为2,还不够长,所以继续找,看“ca”中的“c”是哪个序列变过来的呢,这次我们找到了"cc“还有”ee“,同样代替,那“ca”的上一次状态就是“cca”或者“eea”此时找到,长度为n则答案加1,那这么一直寻找的过程就想到了DF

Codeforces 6D Lizards and Basements 2 dfs+暴力

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

[2016-04-04][codeforces][639][A][Bear and Displayed Friends]

时间:2016-04-04 12:38:53 星期一 题目编号:[2016-04-04][codeforces][639][A][Bear and Displayed Friends] 题目大意:有n朋友,第i个朋友有亲密度ti,有q次命令,1 id表示编号为id的朋友上线,2 id表示是否在线,且亲密度在前k个,输出结果 分析:可以发现k最大为0,只需要维护前k个最大值就ok,每加入一个数字就维护一次最大值 #include <cstring> #include <cstdio>

CodeForces 440C One-Based Arithmetic(递归,dfs)

A - One-Based Arithmetic Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-08-21) Description Prof. Vasechkin wants to represent positive integer n as a sum of adden

[2016-04-04][codeforces][639][B][Bear and Forgotten Tree 3]

时间:2016-04-04 13:11:25 星期一 题目编号:[2016-04-04][codeforces][639][B][Bear and Forgotten Tree 3] 题目大意:一棵树有n个节点,直径为d,直径为h,问,这样的树是否存在,是则输出任意一棵树的所有边 分析: 树的高度是h,那么树的直径最大为2h 所以d?2×hd?2×h 所以只需要生成第一个枝条深度为h,其他枝条深度为d-h的树即可 如果d>2×hd>2×h,那么这样的树就不存在 d == h,剩下的节点不能放在

Codeforces 455C Civilization(并查集+dfs)

题目链接:Codeforces 455C Civilization 题目大意:给定N,M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的,然后是Q次操作,操作分为两种,一种是查询城市x所在的联通集合中,最长的路为多长.二是连接两个联通集合,采用联通之后最长路最短的方案. 解题思路:因为一开时的图是不可以改变的,所以一开始用dfs处理出各个联通集合,并且记录住最大值,然后就是Q次操作,用并查集维护,注意因为联通的时候要采用最长路径最短的方案,所以s的转移方程变为s = max(s,

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {

codeforces Gym 100187J J. Deck Shuffling dfs

J. Deck Shuffling Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/J Description The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and k s

Codeforces Round #290 (Div. 2) B (dfs)

题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相同且已搜过,则存在满足题意的环 代码: 1 #include <bits/stdc++.h> 2 #define MAXN 60 3 using namespace std; 4 5 int mp[MAXN][MAXN], vis[MAXN][MAXN], m, n; 6 int dir[4][2