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 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 暴力的更多相关文章
- 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 ...
- 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) 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左 ...
随机推荐
- es6新语法Object.assign()
1.介绍 Object.assign用于对象的合并,将源对象的所有可枚举属性复制到目标对象,只拷贝源对象自身的属性继承属性补考呗 Object.assign(target,source1,...)第一 ...
- pycharts实现可视化
https://blog.csdn.net/u012535605/article/details/80677791http://pyecharts.org/#/zh-cn/prepare (中文官网 ...
- ASLR pe 分析
ASLR 转:http://www.cnblogs.com/dliv3/p/6411814.html 3ks @author:dlive 微软从windows vista/windows server ...
- 70.如何在xilinx SDK中显示行号
Window→preferences→editor→test editor 对ecilpse的通用方法 打开Eclipse软件,在菜单中选择窗口——首选项,打开新的窗口. 在新的窗口中依次选择常规—— ...
- Ubuntu10.04 下安装RabbitVCS
安装RabbitVCS的方法步骤如下: 1.sudo add-apt-repository ppa:rabbitvcs/ppa #将rabbitvcs的添加到源里面.(次操作会提示是否要添 ...
- 混合式App开发 Apicloud 官方iPhone X 适配
iPhone X 适配 由于iPhone X的特殊造型,为了方便开发者对iPhone X进行适配,苹果在iOS 11中引入了Safe Area的概念,引擎也在api对象下添加了safeArea属性和s ...
- iscsi服务器的搭建
1.在您的存储服务器上,以 root 用户身份使用 yum 命令安装 scsi-t arget -ut ils 软件包. # yum install -y scsi-target-utils 2.把您 ...
- nginx报502修复日志
参考:https://www.baidu.com/link?url=PGd7mgvalnQp0MOVZTyDJIvr6_eJn1hmPlmsLpdj2vH6w3FzMt3pZEd_MKpoiqX1OF ...
- AWS 使用总结
A.升配置的流程: 1.新开一台配置较高的机器; 2.将新机器和老机器的磁盘都取消关联,注意需要记录下老机器的磁盘分区设备名,如:/dev/sda1: 3.将老机器的磁盘挂载到新机器上,磁盘分区设备名 ...
- java通过jdbc插入中文到mysql显示异常(问号或者乱码)
转自:https://blog.csdn.net/lsr40/article/details/78736855 首先本人菜鸡一个,如果有说错的地方,还请大家指出予批评 对于很多初学者来说,中文字符编码 ...