CodeForces Gym 100500A A. Poetry Challenge DFS
2015-07-27 18:31
351 查看
Problem A. Poetry Challenge
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/gym/100500/attachmentsDescription
Let’s check another challenge of the IBM ICPC Chill Zone, a poetry challenge. One says a poetry string that starts with an English letter and ends with an English letter, the second should say a poetry string that starts with the same letter that the previous string ended with.Given the two poetry string sets representing the known strings for each player. Each player can use each of his strings only once. If during the player turn he can not say any string, he loses. Assuming both players play optimally well determine which player wins the game depending on the given two sets.
[b]Input[/b]
The first line contains an integer T represent the number of the following test cases. Each test case starts with an integer n the number of strings in the first player set. Each of the next n lines contains a string of the first player set. Then read an integer m, which will be succeeded by m lines describing the strings of the second player. No string in the input will start or finish with a white space, only lowercase letters. The length of each string in the input will not exceed 10,000 letters. 1 ≤ n ≤ 9 1 ≤ m ≤ 9 1 ≤ T ≤ 10
[b]Output[/b]
For each test case, print one line saying which player should win if they are so clever to play it perfectly and assuming that each one knows the set of the other player. Discarding quotes, print "Game_i:_player1"to denote the wining of the first player or "Game_i:_player2"to denote the win of the second player where ‘i’ represents the game number starting from 1. Replace the underscores with spaces.
[b]Sample Input[/b]
2 3 a poetry string a poetry string starting with a a poetry string ending with a 3 generated word a word ending with b poetry 2 either one or two random string 3 another test case one greatest poetry be the winner
[b]Sample Output[/b]
Game 1: player2 Game 2: player1
HINT
[b]题意[/b]
文字接龙,谁接不下去了,就算谁输
都很聪明的情况下
[b]题解:[/b]
转化成图论之后,直接DFS爆搜就好了!
[b]代码[/b]
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) const int maxn=202501; #define mod 1000000007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* char s1[30][20000]; char s2[30][20000]; vector<int> e[30]; int vis[30]; int dfs(int x) { for(int i=0;i<e[x].size();i++) { if(vis[e[x][i]]) continue; vis[e[x][i]]=1; if(!dfs(e[x][i])) { vis[e[x][i]]=0; return 1; } } return 0; } int main() { int t=read(); for(int cas=1;cas<=t;cas++) { memset(vis,0,sizeof(vis)); for(int i=0;i<30;i++) e[i].clear(); int n=read(); for(int i=0;i<n;i++) gets(s1[i]); int m=read(); for(int i=0;i<m;i++) gets(s2[i]); for(int i=0;i<n;i++) { int len=strlen(s1[i]); for(int j=0;j<m;j++) { if(s1[i][len-1]==s2[j][0]) e[i].push_back(j+9); } } for(int i=0;i<m;i++) { int len=strlen(s2[i]); for(int j=0;j<n;j++) { if(s2[i][len-1]==s1[j][0]) e[i+9].push_back(i); } } int flag=0; for(int i=0;i<n;i++) { vis[i]=1; if(!dfs(i)) { flag=1; break; } vis[i]=0; } if(flag) printf("Game %d: player1\n",cas); else printf("Game %d: player2\n",cas); } }
相关文章推荐
- tornado + nginx + supervisord 环境部署
- 最新java数组的详解
- g++动态库静态库混合链接
- 母函数
- 【读书笔记】UIWindow-WindowLevel
- g++动态库静态库混合链接
- MYCNCART 自适应主题模板 MCCThemes-005
- POJ 1084 Square Destroyer
- findbug笔记
- 绑定远程服务的流程aidl
- 【读书笔记】UIWindow-WindowLevel
- poj 3020 Antenna Placement 二分图匹配
- leetcode刷题 总结 记录 备忘 64
- 关联源码
- codeforce Gym 100500H ICPC Quest (简单dp)
- android学习笔记---63-PopupWindow,泡泡窗口的实现
- 中国澳门sinox很多平台CAD制图、PCB电路板、IC我知道了、HDL硬件描述语言叙述、电路仿真和设计软件,元素分析表
- 排序算法 java实现
- HDU 3529 Bomberman - Just Search!
- 美团Android自动化之旅—生成渠道包