codeforces 653B Bear and Compressing (dfs)
来源:互联网 发布:笔记本强力卸载软件 编辑:程序博客网 时间:2024/04/28 23:49
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. Thei-th operation is described by a stringai of length two and a stringbi of length one. No two ofq possible operations have the same stringai.
When Limak has a string s he can perform thei-th operation ons if the first two letters ofs match a two-letter stringai. Performing thei-th operation removes first two letters ofs 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 anyai.
Limak wants to start with a string of length n and performn - 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 andq (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. Thei-th of them contains two stringsai andbi (|ai| = 2, |bi| = 1). It's guaranteed that ai ≠ aj fori ≠ j and that all ai andbi 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 5ab acc cca aee cff d
4
2 8af edc dcc fbc bda beb abb bff c
1
6 2bb aba a
0
In the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There are4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation1 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".
题意:给出q组数据,每组数据有一个长度为2的字符串a和一个字符bi,表示字符串a可以转化成bi,现在限定一段长度为n的字符串,当它最前的两个字符和前面的数据可以匹配,则可以转换,转换次数不限。现在问有多少段这样的字符串最终可以转化成'a'。
#include <stdio.h>#include <algorithm>#include <string.h>#include <string>#include <stack>#include <map>using namespace std;char s[] = {'a', 'b', 'c', 'd', 'e', 'f'}, a[10];map<string, char> mp;int n, q, sum;bool check(){ stack<char> ss; for(int i = n - 1;i >= 0;i--) ss.push(a[i]); while(!ss.empty()){ if(ss.size()==1){ if(ss.top() == 'a'){ //puts(a); sum++; return 1; } else return 0; } char a = ss.top(); ss.pop(); char b = ss.top(); ss.pop(); string v; v += a; v += b; map<string, char>::iterator it = mp.find(v); if(it == mp.end()) return 0; ss.push(it->second); }}bool dfs(int num){ if(num == n){ a[num] = '\0'; //puts(a); if(check()) return 1; return 0; } for(int i = 0;i < 6;i++){ a[num] = s[i]; dfs(num+1); } return 0;}int main(){ int i; char a[5], b[5]; scanf("%d %d", &n, &q); for(i = 0;i < q;i++){ scanf("%s %s", a, b); mp.insert(pair<string, char>(a, b[0])); } dfs(0); printf("%d\n", sum);}
- Codeforces 653B Bear and Compressing【DFS】
- codeforces 653B Bear and Compressing (dfs)
- Codeforces 653B Bear and Compressing
- Codeforces 653B Bear and Compressing 【数位dp】
- Codeforences #653B Bear and Compressing
- CF 653B. Bear and Compressing
- cf 653B Bear and Compressing
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) 653B Bear and Compressing(dfs + stl)
- codeforces 679B Bear and Tower of Cubes(贪心+dfs)
- Codeforces Round #405 B.Bear and Friendship Condition【Dfs+思维】
- (DFS, 图)Codeforces Round #405 B. Bear and Friendship Condition
- codeforces 679B Bear and Tower of Cubes(贪心+dfs)
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing 暴力
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)B. Bear and Compressing
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)-B - Bear and Compressing-暴力DP
- Codeforces 385B Bear and Strings
- CodeForces 574B-Bear and Three Musketeers
- 【LeetCode】Patching Array
- kickstart无人值守,PXE+TFTP+FTP+DHCP
- Poj1503(高精度加法)
- [Other]1/0.0f Error?
- 【JAVA】算法排序(一)——选择排序法
- codeforces 653B Bear and Compressing (dfs)
- Ubuntu搭建Openstack平台(kilo)(七.Dashboard)
- HDU 2043 密码(判断字符串存在性)
- ACM刷题之HDU————献给杭电五十周年校庆的礼物
- 第十周第十一周阅读程序-5-d
- xshow-1. 项目简介
- eclipse中 maven 环境搭建
- 冒泡排序
- Clone Graph