Problem A. Poetry Challenge
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100500/attachments
Description
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.
Input
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
Output
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.
Sample Input
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
Sample Output
Game 1: player2 Game 2: player1
HINT
题意
从player1开始进行字母接龙游戏,接不下去的输,问最后谁赢了
题解:
转化成点与点相接,dfs....... 感谢小q神的博客
代码
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <queue> 10 #include <typeinfo> 11 #include <map> 12 #include <stack> 13 typedef long long ll; 14 using namespace std; 15 inline ll read() 16 { 17 ll x=0,f=1; 18 char ch=getchar(); 19 while(ch<'0'||ch>'9') 20 { 21 if(ch=='-')f=-1; 22 ch=getchar(); 23 } 24 while(ch>='0'&&ch<='9') 25 { 26 x=x*10+ch-'0'; 27 ch=getchar(); 28 } 29 return x*f; 30 } 31 //************************************************************************************** 32 vector<int > e[1000]; 33 bool vis[2000]; 34 bool dfs(int x) 35 { 36 for(int i=0; i<e[x].size(); i++) 37 { 38 if(vis[e[x][i]])continue; 39 vis[e[x][i]]=1; 40 if(!dfs(e[x][i])) 41 { 42 vis[e[x][i]]=0; 43 return 1; 44 } 45 } 46 return 0; 47 } 48 int main() 49 { 50 int oo=1; 51 int T; 52 scanf("%d",&T); 53 while(T--) 54 { 55 memset(vis,0,sizeof(vis)); 56 for(int i=0; i<30; i++) 57 e[i].clear(); 58 int n=read(); 59 char a[222]; 60 char s1[22][2000]; 61 char s2[22][2000]; 62 for(int i=1; i<=n; i++) 63 { 64 gets(s1[i]); 65 66 } 67 int m=read(); 68 for(int i=1; i<=m; i++) 69 gets(s2[i]); 70 for(int i=1; i<=n; i++) 71 { int l=strlen(s1[i]); 72 for(int j=1; j<=m; j++) 73 { 74 75 if(s1[i][l-1]==s2[j][0]) 76 e[i].push_back(j+10); 77 } 78 } 79 for(int i=1; i<=m; i++) 80 { int l=strlen(s2[i]); 81 for(int j=1; j<=n; j++) 82 { 83 84 if(s2[i][l-1]==s1[j][0]) 85 e[i+10].push_back(j); 86 } 87 } 88 bool flag=false; 89 for(int i=1; i<=n; i++) 90 { 91 vis[i]=1; 92 if(!dfs(i)) 93 { 94 flag=true; 95 break; 96 } 97 vis[i]=0; 98 } 99 if(flag) 100 printf("Game %d: player1 ",oo++); 101 else 102 printf("Game %d: player2 ",oo++); 103 } 104 return 0; 105 }