IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) 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.
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.
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".
这是我的第一篇博客。 感觉这道题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的更多相关文章
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- 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 ...
- 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 ...
- 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 ...
- 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) A. Bear and Three Balls 水题
A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...
- 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 ...
- 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 ...
- IndiaHacks 2016 - Online Edition (CF) . D
这题思路很简单,二分m,求最大流是否大于等于x. 但是比赛过程中大部分的代码都被hack了... 精度问题,和流量可能超int 关于精度问题,这题真是提醒的到位,如果是先用二分将精度控制在10^-8左 ...
随机推荐
- Primes on Interval
AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algori ...
- LeetCode OJ 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 有indexPath获取到cell对象
//SHSecretaryTipsCell *cell=(SHSecretaryTipsCell *)[tableView cellForRowAtIndexPath:indexPath];
- s7-300 第二讲
- CentOS6.6 搭建Zabbix_3.0
公司有下发内网监控服务器的需求 使用zabbix监控 所以这篇文章是讲述的zabbix的搭建 其实网上很多地方都有 可以参考 环境安装 系统环境: # cat /etc/redhat-release ...
- byte数组与int,long,short,byte转换 (转载)
byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteO ...
- DB9 公头母头引脚定义及连接、封装
DB9 公头母头引脚定义及连接.封装 转自:http://blog.csdn.net/yangshuodianzi/article/details/8997478 1.实物及引脚简介 在做开发的时候经 ...
- 关于项目刚才还能运行的,重启Myeclipse就不能运行(报错)的解决方法
这个是以为内存不足引起的,就是打开MyEclipse的时候,因为内存不足,没有加载完整的项目,这个时候需要重启电脑,即可解决问题.
- FTP: Configuring server users..
4 points to create a user to uploade to ftproot.. this user must be an administrator, and be able to ...
- Android图片处理神器BitmapFun源码分析
作为一名Android开发人员,相信大家对图片OOM的问题已经耳熟能详了,关于图片缓存和解决OOM的开源项目也是相当的多,被大家熟知的就是Universal_image_loader和Volley了, ...