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 that ai ≠ 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 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 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".

思路:数据范围很小,枚举出所有字符串逐一验证。

DFS:

 #include "cstdio"
 #include "iostream"
 #include "algorithm"
 #include "string"
 #include "cstring"
 #include "queue"
 #include "cmath"
 #include "vector"
 #include "map"
 #include "stdlib.h"
 #include "set"
 #define mj
 #define db double
 #define ll long long
 using  namespace std;
 ;
 ;
 ;
 ][],b[][],c[];
 int n,m;
 ;
 void dfs(int x){
     if(x>=n){
         ];
         int j;
         ;i<n;i++){
             ;j<m;j++){
                 ]&&c[i]==a[j][])
                 {e=b[j][];break;}

             }
             if(j==m) return ;
         }
         if(e=='a') ans++;return;
     }
     );
 }
 int main()
 {
     scanf("%d%d",&n,&m);
     ;i<m;i++){
         scanf("%s%s",a[i],b[i]);
     }
     dfs();
     printf("%d\n",ans);
     ;

 }

635B. Bear and Compressing的更多相关文章

  1. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing

    B. Bear and Compressing 题目链接  Problem - B - Codeforces   Limak is a little polar bear. Polar bears h ...

  2. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing 暴力

    B. Bear and Compressing 题目连接: http://www.codeforces.com/contest/653/problem/B Description Limak is a ...

  3. codeforces 653B B. Bear and Compressing(dfs)

    题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. Codeforces 653B Bear and Compressing【DFS】

    题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...

  5. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Saddest's polar bear Pizza offered new YorkShire home

    Saddest:adj,可悲的,悲哀的,polar,两级的,极地额,YorkShire,约克郡 A UK wildlife park has confirmed that it is offering ...

  8. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  9. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections 优先队列

                                                    A. Bear and Elections                               ...

随机推荐

  1. BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草(动态规划)

    裸的背包= =,没什么好说的= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#incl ...

  2. Tomcat8 + Redis实现session集中管理

      环境准备:   部署两台 tomcat 8.0   安装 redis 服务器   下载工具库( commons-pool2-2.3.jar.jedis-2.7.2.jar .改良版的 tomcat ...

  3. Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用

    前面做了一个非常垃圾的小demo,真的无法直面它,菜的抠脚啊,真的菜,好好努力把.菜鸡. --WH 一.JDBC是什么? Java Data Base Connectivity,java数据库连接,在 ...

  4. 北邮OJ

    90. 字符串转换 时间限制 1000 ms 内存限制 65536 KB 题目描述 我们将仅由若干个同一小写字母构成的字符串称之为简单串,例如"aaaa"是一个简单串,而" ...

  5. 分享学习——ERP项目管理经验

    为什么在实施过程中有的项目就能做的非常好,有的项目应用效果就非常差?原因在哪里?下面本人就从下面几个方面进行分析: 1.什么是项目? 2.在ERP软件行业项目应该怎么做? 3.为什么有一些项目会失败, ...

  6. 搭建hibernate环境

    Hibernate概述什么是框架1 写程序,使用框架之后,帮我们实现一部分功能,使用框架好处,少写一部分代码实现功能 什么是hibernate框架(重点)1 hibernate框架应用在javaee三 ...

  7. zBase --轻量级DOM操作库

    项目地址:ZengTianShengZ-github zBase-1.2.0 --v3 修复部分bug,添加AMD规范测试 zBase-1.1.0 --v2 对 v1 版本做了升级,优化DOM查找,简 ...

  8. CSS实现覆盖弹窗(效果如JQuery-UI的Dialog)

    原理:定义一个新的div用来覆盖整个页面,再把想要弹出的窗口放在这个div上面 1.定义一个div,设置其隐藏(display:none),用于覆盖整个页面,并设置其CSS属性为: #divBg { ...

  9. BFC原理

    一.BFC是什么? 在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说 ...

  10. Codeblocks快捷键

    Codeblocks快捷键: Ctrl+Shift+Enter:复制控制台exe内容 Ctrl+Z:撤销 Ctrl+F:查找  Ctrl+R:查找替换功能 Ctrl+Shift+C:加注释     C ...