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. IOS缓存管理之YYCache使用

    前言: 最近一直在致力于为公司app添加缓存功能,为了寻找一个最佳方案,这几天先做个技术预研,经过这两天的查找资料基本上确定了两个开源框架进行选择,这两个开源框架分别是:PINCache.YYCach ...

  2. Linux下软件设成系统服务运行

    将软件的启动脚本copy到/etc/init.d/soft_name             chmod 777 soft_name   设置服务开机启动   chkconfig soft_name ...

  3. 用js,css3 做的一个球

    用css3属性很容易做一个立方体,但是要做一个球体,会相对复杂些 原理是:球可以看做是由无数个圆圈构成,然后就可以用圆圈来做球, 下面的例子是我做的一个小球,由72个圆圈组成.如果把每个圆圈的背景颜色 ...

  4. redux:applyMiddleware源码解读

    前言: 笔者之前也有一篇关于applyMiddleware的总结.是applyMiddleware的浅析. 现在阅读了一下redux的源码.下面说说我的理解. 概要源码: step 1:  apply ...

  5. Java面试03|并发及锁

    1.synchronized与Lock的区别 使用synchronized这个关键字实现的同步块有一些缺点: (1)锁只有一种类型 (2)线程得到锁或者阻塞 (3)Lock是在Java语言层面基于CA ...

  6. JAVA基础:自己构造一个按递增排列的数组,用户输入一个数,插入适当位置

  7. 如何快速部署国人开源的 Java 博客系统 Tale

    除了闷头专研技术之外,程序员还需要不断地写作进行技术积累,写博客是其中最重要的方式之一.商业博客平台不少,但是更符合程序员背景的方案,是自己开发一个博客平台或者使用开源的博客平台. 开源的博客平台多如 ...

  8. jenkins gradle 编译遇到tomcat异常

    使用gradle在jenkins 上构建的时候遇到 进行构建的时候报"Could not load Logmanager org.apache.juli.ClassLoaderLogMana ...

  9. 腾讯云数据库团队:SQL Server 数据加密功能解析

    数据加密是数据库被破解.物理介质被盗.备份被窃取的最后一道防线:数据加密,一方面解决数据被窃取安全问题,另一方面有关法律要求强制加密数据:SQL Server 的数据加密相较于其他数据库,功能相对完善 ...

  10. windows phone 8.1开发SQlite数据库引用安装

    原文出自:http://www.bcmeng.com/windows-phone-sqlite/ windows phone 8.1开发SQlite数据库引用安装 第一步: 安装SQlite forw ...