CF456D A Lot of Games (字典树+DP)
CF#260 Div2 D题
CF#260 Div1 B题
Codeforces Round #260
CF455B
D. A Lot of Games
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputAndrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move. Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him. Input
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109). Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters. Output
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes). Sample test(s)
Input
2 3 Output
First Input
3 1 Output
First Input
1 2 Output
Second |
题意:两个人玩游戏。游戏是拼单词,给出了一个含有n个单词的词库。当前单词初始为空,每个人轮流给它末尾加一个字母,要求当前单词是词库中任意一个词的前缀,当某个人不能加字母了他就输。这个游戏重复k局,一局输的人下一局先手,最后一局定胜负。两个人都不蠢,求第一局先手是否能保证最后能赢,能就输出First,不能则输出Second。
题解:
Trie树+树DP。
先把词典各词加入Trie树,则Trie树每个节点都为游戏中的某一步,游戏就变成从这棵树树根开始轮流向下走一步,看谁能走到叶子。
这样,每个节点可能有4种状态,分别是必赢、必输、能输能赢(想怎样就怎样)、不能输不能赢(走这步会让对面变得能输能赢)。
叶子节点都是必赢。只有一个必赢儿子的点必输,只有一个必输儿子的点必赢。接下来我们主要考察有多个儿子的特殊情况。
我们可以统计儿子有哪些种类,即统计儿子中是否有能输能赢点、不能输不能赢点、必输点、必赢点,
1.若儿子中有能输能赢点,则对手肯定怒走那个点,则当前这个点是不能输不能赢点。
2.若儿子中又有赢点又有输点,对手可以随便选,则当前这个点也是不能输不能赢点。
3.若儿子中只有不能输不能赢点,则对手只能走这个逗点,则当前这个点是能输能赢点。
4.若儿子只有赢点,当前点肯定是输点;若儿子只有输点,当前点肯定是赢点。
这样,我们一遍dfs就可以得到所有点的状态,简直树DP。
最后简单判断一下到底第一局先手能不能赢。
1.假如每局先手能输能赢,则第一局先手的人可以连输k-1场,保证先手权,最后一场赢。
2.假如每局先手必赢,则后手必输,先手想输也输不掉,这样两个人轮流赢,若k是奇数,则第一局先手的人最后一局赢。
3.除了这些情况之外,先手都不能必赢。
代码:
1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cstring> 6 #include<algorithm> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<queue> 12 using namespace std; 13 #define ll long long 14 #define usint unsigned int 15 #define mz(array) memset(array, 0, sizeof(array)) 16 #define minf(array) memset(array, 0x3f, sizeof(array)) 17 #define REP(i,n) for(i=0;i<(n);i++) 18 #define FOR(i,x,n) for(i=(x);i<=(n);i++) 19 #define RD(x) scanf("%d",&x) 20 #define RD2(x,y) scanf("%d%d",&x,&y) 21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) 22 #define WN(x) printf("%d\n",x); 23 #define RE freopen("D.in","r",stdin) 24 #define WE freopen("1biao.out","w",stdout) 25 26 const int maxn=111111; 27 int n,k; 28 char s[maxn]; 29 int chd[maxn][26]; 30 int canwin[maxn],canlose[maxn]; 31 int tn; 32 int ID[128]; 33 34 void insert(char *s) { 35 int len=strlen(s),i; 36 int now=0; 37 for(i=0; i<len; i++) { 38 if(!chd[now][ID[s[i]]]) chd[now][ID[s[i]]]=tn , now=tn++; 39 else now=chd[now][ID[s[i]]]; 40 } 41 } 42 43 void dfs(int x) { 44 int i; 45 bool last=true; 46 bool cwin=0,close=0,canwinlose=0,cant=0; 47 for(i=0; i<26; i++) { 48 if(chd[x][i]!=0) { 49 //printf("%c:",'a'+i); 50 dfs(chd[x][i]); 51 last=false; 52 if(canwin[chd[x][i]]){///统计儿子有4种点中的哪些点 53 if(canlose[chd[x][i]])canwinlose=1; 54 else cwin=1; 55 } 56 else { 57 if(canlose[chd[x][i]]) close=1; 58 else cant=1; 59 } 60 } 61 } 62 canwin[x]=0;canlose[x]=0;///初始化为0,0 63 if(last)canwin[x]=1; 64 else{ 65 if(canwinlose==0){///如果儿子里有有能赢又能输的点,当前点肯定是0,0,没有这种点才继续看 66 if(!(cwin && close)){///如果儿子里又有赢点又有输点,当前点肯定是0,0,不是这种情况才继续看 67 if(cant && !cwin && !close)///如果儿子只有0,0点,当前点肯定是1,1点 68 canwin[x]=1,canlose[x]=1; 69 else if(cwin)///如果儿子只有赢点,当前点肯定是输点 70 canlose[x]=1; 71 else if(close)///儿子只有输点,当前点肯定是赢点 72 canwin[x]=1; 73 } 74 } 75 } 76 //printf("(%d,%d)",canwin[x],canlose[x]); 77 } 78 79 int main() { 80 int i,j; 81 REP(i,26) { 82 ID[i+'a']=i; 83 } 84 while(scanf("%d%d",&n,&k)!=EOF) { 85 mz(chd); 86 tn=1; 87 for(i=0; i<n; i++) { 88 scanf("%s",s); 89 insert(s); 90 } 91 canwin[0]=0;canlose[0]=0; 92 for(i=0; i<26; i++) {///众起点中有赢点,走这个点就必赢;有输点,走这个点就必输。 93 if(chd[0][i]!=0) { 94 dfs(chd[0][i]); 95 if(canwin[chd[0][i]])canwin[0]=1; 96 if(canlose[chd[0][i]])canlose[0]=1; 97 //printf("%c,%d,%d\n",'a'+i,canwin[chd[0][i]],canlose[chd[0][i]]); 98 } 99 } 100 //printf("%d,%d\n",canwin[0],canlose[0]); 101 bool first=false; 102 if(canwin[0]) 103 if(canlose[0]) first=true;///能输能赢,乃真正法器(连输,最后赢一局) 104 else if((k&1)==1)first=true;///能赢不能输,则必轮流赢 105 ///其他情况不能必赢 106 if(first)puts("First"); 107 else puts("Second"); 108 } 109 return 0; 110 }