码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces Gym 100500A A. Poetry Challenge DFS

时间:2015-07-27 20:35:38      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 20 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

 

题意

文字接龙,谁接不下去了,就算谁输

都很聪明的情况下

题解:

转化成图论之后,直接DFS爆搜就好了!

代码

#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);
    }
}

 

CodeForces Gym 100500A A. Poetry Challenge DFS

标签:

原文地址:http://www.cnblogs.com/qscqesze/p/4680914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!