• B. Bear and Compressing
 

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".

这是我的第一篇博客。 感觉这道题bfs运用的非常巧妙。

从a出发,先把2个字符替换一个字符a的字符串中的第一个字符加入队列,以此字符为基点,进行搜索。

 #include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
int a[][];
struct node{
int x,k;
};
node nod;
queue<node> q;
void input(){
int n,b;
char s1[],s2[];
int d,c;
scanf("%d%d",&n,&b);
for(int i = ; i<=b; i++)
{
scanf("%s%s",s1,s2);
d = s2[] - 'a' + ;
c = s1[] - 'a' + ;
a[d][c] += ;
}
int ans = ;
for(int j = ; j<=; j++){
if(a[][j]){
for(int i = ; i<=a[][j]; i++){
nod.x = j;
nod.k = ;
q.push(nod);
} }
}
while(!q.empty()){
nod = q.front();
q.pop();
if(nod.k == n) ans++;
if(nod.k>n) break;
int x = nod.x;
int k = nod.k;
for(int j = ; j<=; j++){
if(a[x][j]){
for(int i = ; i<=a[x][j]; i++){
nod.x = j;
nod.k = k+;
q.push(nod);
} }
}
}
printf("%d\n",ans);
}
int main()
{
input();
return ;
}

IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing的更多相关文章

  1. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表

    E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...

  2. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树

    E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...

  3. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) D. Delivery Bears 二分+网络流

    D. Delivery Bears 题目连接: http://www.codeforces.com/contest/653/problem/D Description Niwel is a littl ...

  4. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down 暴力

    C. Bear and Up-Down 题目连接: http://www.codeforces.com/contest/653/problem/C Description The life goes ...

  5. 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 ...

  6. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题

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

  7. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)——A - Bear and Three Balls(unique函数的使用)

    A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))

    传送门 A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. IndiaHacks 2016 - Online Edition (CF) . D

    这题思路很简单,二分m,求最大流是否大于等于x. 但是比赛过程中大部分的代码都被hack了... 精度问题,和流量可能超int 关于精度问题,这题真是提醒的到位,如果是先用二分将精度控制在10^-8左 ...

随机推荐

  1. servlet第2讲(下集)----创建servlet实例(继承GenericServlet)

  2. 其它网页可以上网,IE浏览器打不开网页的解决办法

    下面是自己引用别人的,作为自己的备注 昨天由于安装了多款软件,今天开机发现IE浏览器打不开了,废了些周折终于,修复了IE浏览器,现将ie浏览器打不开网页的经验分享给大家,希望此经验对于出现过此类情况的 ...

  3. 在Linux服务器上增加硬盘没那么简单【转】

    运维案例:HP服务器,LINUX系统在保障数据的前提下扩展/home分区 部门需求:研发部门提出需要在现有的服务器上扩容磁盘空间,以满足开发环境的磁盘需求.现有空间1.6T需要增加到2T. 需求调查分 ...

  4. Inno Setup入门(十七)——Inno Setup类参考(3)

    分类: Install Setup 2013-02-02 11:28 433人阅读 评论(0) 收藏 举报 标签 标签(Label)是用来显示文本的主要组件之一,也是窗口应用程序中最常用的组件之一,通 ...

  5. mysql 中tinytext、text、mediumtext和longtext详解

    一.数字类型 类型 范围 说明   Char(N) [ binary] N=1~255 个字元binary :分辨大小写 固定长度 std_name cahr(32) not null VarChar ...

  6. oracle登陆,在监听服务启动了的情况下,登陆用户还是报错未启动监听服务的错误(刚开始装oracle是能登陆的,重启之后装了plsql)

    刚开始装oracle是能登陆的,重启之后装了140M的plsql,所以有可能是plsql跟oracle监听冲突了,所以我之后换了33M的plsql,oracle就没问题了,可以正常登陆了

  7. linux 查看 cpu 和内存的命令 - top

    1.查看内存,cpu ,当前进程task数目, 每个进程的cpu, 内存使用率, 用top 命令: 在这个页面,按 P,下面的进程排序,以cpu使用率降序排列. 按M,按内存使用率降序排列: 按N, ...

  8. OpenGL----绘制立方体,定点数组与顶点缓冲

    ,立方体是很简单,但是这里只是拿立方体做一个例子,来说明OpenGL在绘制方法上的改进.从原始一点的办法开始一个立方体有六个面,每个面是一个正方形,好,绘制六个正方形就可以了. glBegin(GL_ ...

  9. Spring Boot 系列教程11-html页面解析-jsoup

    需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...

  10. ReactiveCocoa的冷信号与热信号 探讨

    背景 ReactiveCocoa(简称RAC)是最初由GitHub团队开发的一套基于Cocoa的FRP框架.FRP即Functional Reactive Programming(函数式响应式编程), ...