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左 ...
随机推荐
- 编辑器phpstrom的快捷键修改
file->setting-->查找 keymap -->查找 format 格式化代码 ctrl+alt +L appearance-->外观-->显示行号
- word、pdf、ppt 转为图片
office word文档.pdf文档.powerpoint幻灯片是非常常用的文档类型,在现实中经常有需求需要将它们转换成图片 -- 即将word.pdf.ppt文档的每一页转换成一张对应的图片,就像 ...
- hdu 1425 sort
Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行 ...
- 第3章 Java语言基础----成员变量与局部变量
在对局部变量进行赋值时,不能对非静态字段age进行静态引用,图1错误,加上static后图二正确,图3与图4类似,如下图所示: 图1图2 图3图4 2.成员变量times在类中定义,局部变量times ...
- away 3d的一些问题
不能成功draw m3u8视频流问题: Texture2DBase.as return context.createRectangleTexture(_width, _height, Context3 ...
- [实用]DNS解析命令,静静地学会【转载】
[实用]DNS解析命令,静静地学会 2016-08-04 06:50 一.Windows下的nslookup 简单的查某个域名,那就nslookup toutiao.com,上面是dns地址,下面是解 ...
- 个人linux简单笔记,随时更新
vim显示行数 :set nu 查找文件 find /home -name config.txt 重命名文件或者文件夹 mv a b centos中phpize的安装 yum install php- ...
- 其他信息: Error creating context 'spring.root': 未能加载文件或程序集“EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项
详细错误情况: “System.Configuration.ConfigurationErrorsException”类型的异常在 Spring.Core.dll 中发生,但未在用户代码中进行处理 其 ...
- Windows下搭建PHP开发环境【总结】
一.准备工作-下载所需软件 Apache 进入apache服务器官网http://httpd.apache.org/ ,下面是下载的教程:http://jingyan.baidu.com/articl ...
- php 使用操作符
算术操作符.字符串操作符.赋值操作符 1.比较操作符 比较运算符 例子 名称 结果 $a == $b 等于 TRUE,如果类型转换后 $a 等于 $b. $a === $b 全等 TRUE,如果 $a ...