B. Bear and Compressing

题目连接:

http://www.codeforces.com/contest/653/problem/B

Description

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.

Sample Input

3 5

ab a

cc c

ca a

ee c

ff d

Sample Output

4

Hint

题意

你需要构造一个长度为n的串,使得经过n-1次变换之后得到a

现在你可以进行m种变换

变换描述如下:如果首两个字符为b1b2,那么你可以变成c

问你一共有多少种构造方案。

题解:

数据范围n=6,所以直接dfs就好了,数据范围很小的。

代码

#include<bits/stdc++.h>
using namespace std;
int n,q;
int x[50],y[50],z[50];
vector<int>ans;
int Ans=0;
void dfs(int x1)
{
if(x1==n)
{
int flag = 0;
int now = ans[0];
for(int i=1;i<ans.size();i++)
{
int flag2 = 1;
for(int j=0;j<q;j++)
{
if(now==x[j]&&ans[i]==y[j])
{
flag2 = 0;
now = z[j];
break;
}
}
if(flag2)
return;
}
if(now==0)Ans++;
return;
}
for(int i=0;i<6;i++)
{
ans.push_back(i);
dfs(x1+1);
ans.pop_back();
}
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=0;i<q;i++)
{
string x1,y1;
cin>>x1>>y1;
x[i]=(int)(x1[0]-'a');
y[i]=(int)(x1[1]-'a');
z[i]=(int)(y1[0]-'a');
}
dfs(0);
cout<<Ans<<endl;
}

IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. 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) E - Bear and Forgotten Tree 2 链表

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

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

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

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

  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. struts2 constant详解

    <!-- 指定Web应用的默认编码集,相当于调用 HttpServletRequest的setCharacterEncoding方法 -->        <constant nam ...

  2. USB 3.0传输规格

    通用序列总线(USB) 从1996问世以来,一统个人电脑外部连接界面,且延伸至各式消费性产品,早已成为现代人生活的一部分.2000年发表的USB 2.0 High-speed规格,提供了480Mbps ...

  3. ltib安装过程中遇到好多问题,从网上转来的好多份总结

    最近调试MPC5125的板子,第一步LTIB都装不过去,挫败感十足. LTIB的安装镜像来自于freescale的ltib-mpc5121ads-200906,是用于Ubuntu 10版本之前的,现在 ...

  4. 「caffe编译bug」 undefined reference to `boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11

    CXX/LD -o .build_release/tools/test_net.binCXX/LD -o .build_release/tools/convert_annoset.binCXX/LD ...

  5. [转载]FFmpeg完美入门[4] - FFmpeg应用实例

    1 用FFserver从文件生成流媒体 一.安装ffmpeg 在ubuntu下,运行sudo apt-get ffmpeg 安装ffmpeg,在其他linux操作系统下,见ffmpeg的编译过程(编译 ...

  6. Enumeration的学习

    枚举是jdk5.0之后的新特性.枚举的使用在编程中能起到很大的作用,本文从枚举的适用范围.枚举的特点.枚举的使用等三个方面学习枚举 一.枚举的使适用范围 “在有限的范围内选择值”:比如一个星期只有星期 ...

  7. Mac——mac安装软件

    命令行: perl: curl -L http://xrl.us/installperlosx | bash 参考资料: https://blog.csdn.net/yuxin6866/article ...

  8. Java的Timer定时器

    Timer主要用于Java线程里指定时间或周期运行任务,它是线程安全的,但不提供实时性(real-time)保证. 上面提到了守护线程的概念. Java分为两种线程:用户线程和守护线程. 所谓守护线程 ...

  9. C#串口serialPort操作

    现在大多数硬件设备均采用串口技术与计算机相连,因此串口的应用程序开发越来越普遍.例如,在计算机没有安装网卡的情况下,将本机上的一些信息数据 传输到另一台计算机上,那么利用串口通信就可以实现.运行本程序 ...

  10. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...