codeforces 653B B. Bear and Compressing(dfs)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
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.
3 5
ab a
cc c
ca a
ee c
ff d
4
2 8
af e
dc d
cc f
bc b
da b
eb a
bb b
ff c
1
6 2
bb a
ba a
0
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[],n,q,ans=;
char s[][];
int dfs(char x,int num)
{
if(num>=n-){ans+=a[x-'a'];return ;}
for(int i=;i<q;i++)
{
if(s[i][]==x)
{
char y=s[i][];
dfs(y,num+);
}
}
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<q;i++)
{
scanf("%s",s[i]+);
scanf("%s",s[i]+);
a[s[i][]-'a']++;
}
dfs('a',);
cout<<ans<<endl; return ;
}
codeforces 653B B. Bear and Compressing(dfs)的更多相关文章
- 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 ...
- 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 ...
- 635B. Bear and Compressing
B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 653B Bear and Compressing【DFS】
题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...
- codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)
题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- codeforces 580C Kefa and Park(DFS)
题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...
- Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)
题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...
随机推荐
- 程序猿学英语—In August the English learning summary
时间真快,总结的7月份英语学习总结刚刚结束,转眼间又该对8月份的英语学习进行小节了. 进入8月初.下发了一个文档,用一个星期的时间学音标.纠音. 在王美的带领下我组也发起了纠音运动. 刚开 始纠音的时 ...
- php序列化与反序列化
php序列化简单来说就是 把复杂的数据类型压缩到一个字符串中php对象转换成字符串,反序列化就是把变量转换成对象 一般来说 当把这些序列化的数据放在URL中在页面之间会传递时,需要对这些数据调用ur ...
- 一篇很不错的关于WPF DataGrid的文章,包含validation
https://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples
- Lumen开发:结合Redis实现消息队列(2)
上一篇讲了Lumen配置Redis,现在来讲一下,如何实现消息队列 2.编写任务类 2.1 任务类结构 默认情况下,应用的所有队列任务都存放在app/Jobs目录.任务类非常简单,正常情况下只包含一 ...
- 【BZOJ2337】[HNOI2011]XOR和路径 期望DP+高斯消元
[BZOJ2337][HNOI2011]XOR和路径 Description 题解:异或的期望不好搞?我们考虑按位拆分一下. 我们设f[i]表示到达i后,还要走过的路径在当前位上的异或值得期望是多少( ...
- F - 数论
F - 数论 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
- 在安装mysql数据库的过程中,显示msvcp100.dll丢失?则么办?
方案一:重装操作系统为windows10专业版 方案二:问题: 解答: 报错原因是VC运行库不全或者没有安装导致,百度搜索VC集合下载安装, 链接:https://pan.baidu.com/s/1U ...
- dockerfile nginx配置
Dockerfile 代码 From hub.c.163.com/public/nginx:1.2.1 RUN rm -v /etc/nginx/nginx.conf ADD nginx.conf / ...
- 读:Instance-aware Image and Sentence Matching with Selective Multimodal LSTM
摘要:有效图像和句子匹配取决于如何很好地度量其全局视觉 - 语义相似度.基于观察到这样的全局相似性是由图像(对象)和句子(词)的成对实例之间的多个局部相似性的复合聚集,我们提出了一个实例感知图像和句子 ...
- 关于随机浏览头伪装fake-UserAgent
使用: from fake_useragent import UserAgent ua = UserAgent() #ie浏览器的user agent print(ua.ie) Mozilla/5.0 ...