(CF) D. A Lot of Games
2014-08-09 19:04
316 查看
D. A Lot of Games
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Andrew, 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
output
input
output
input
output
题意:有n个字符串,玩k次游戏,每次每个人添加一个小写字母于字符串屁股后,此字符串必须是给出n个字符串之一的前缀,玩不下去的算输。此为一次游戏局面的输赢,这次输的下次先手。为先手能不能保证第k次能赢。
题解:假设此游戏有两种局面,可赢,可输。如果先手确定不能赢,不管怎么样都赢不了了。如果先手能赢能输,那么可以前面一直输输输,保证先手,然后到第k局再爆发,赢了!如果先手只有赢的局面,那么就得看k是奇数或者偶数了。好了。现在的问题就变成怎么样知道这个局面的先手输赢。在这里我看了别人大神的代码。先把字符串状态存起来,然后根据博弈思想DP求得先手的输赢情况。具体参详。。。或者用字典树也可以求出来。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Andrew, 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 a b
output
First
input
3 1 a b c
output
First
input
1 2 ab
output
Second
题意:有n个字符串,玩k次游戏,每次每个人添加一个小写字母于字符串屁股后,此字符串必须是给出n个字符串之一的前缀,玩不下去的算输。此为一次游戏局面的输赢,这次输的下次先手。为先手能不能保证第k次能赢。
题解:假设此游戏有两种局面,可赢,可输。如果先手确定不能赢,不管怎么样都赢不了了。如果先手能赢能输,那么可以前面一直输输输,保证先手,然后到第k局再爆发,赢了!如果先手只有赢的局面,那么就得看k是奇数或者偶数了。好了。现在的问题就变成怎么样知道这个局面的先手输赢。在这里我看了别人大神的代码。先把字符串状态存起来,然后根据博弈思想DP求得先手的输赢情况。具体参详。。。或者用字典树也可以求出来。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <queue> #include <map> #include <stack> #include <list> #include <vector> using namespace std; #define LL __int64 char s[100010]; int dp[200010][30]; int win[200010],lose[200010]; int main() { int n,k,i,j,m,t; scanf("%d%d",&n,&k); getchar(); m=0; memset(dp,0,sizeof(dp)); for (i=0;i<n;i++) { gets(s); t=0; int l=strlen(s); for (j=0;j<l;j++) { int p=s[j]-'a'; if (dp[t][p]==0) dp[t][p]=++m; t=dp[t][p]; } } memset(win,0,sizeof(win)); memset(lose,0,sizeof(lose)); for (i=m;i>=0;i--) { int f=1; for (j=0;j<26;j++) if (dp[i][j]) { f=0; break; } if (f) { win[i]=0; lose[i]=1; continue; } for (j=0;j<26;j++) if (dp[i][j]) { if (!win[dp[i][j]]) win[i]=1; if (!lose[dp[i][j]]) lose[i]=1; } } if (!win[0]) puts("Second"); else if (lose[0]) puts("First"); else { // 只能赢。 if (k % 2==1) puts("First"); else puts("Second"); } return 0; }
相关文章推荐
- CF 260 DIV2 D. A Lot of Games(字典树+博弈)
- CF 455B(A Lot of Games-树上博弈)
- CF 455B-- A Lot of Games
- Codeforces #260 (Div. 1) B. A Lot of Games(数据结构:trie+DFS)
- codeforces content#260 D A Lot of Games
- CF455B- A Lot of Games(Trie树+DFS+博弈)
- Codeforces Round #260 (Div. 1) B. A Lot of Games Trie + 博弈
- Codeforces Round #260 (Div. 2) D. A Lot of Games
- CF455B - A Lot of Games(Trie树+博弈)
- A Lot of Games - CodeForces #260 (Div. 2) D Trie树
- A Lot of Games(Trie树 + 博弈)
- Codeforces Round #260 (Div. 1) --B. A Lot of Games (Trie)
- Codeforces Round #260(Div2) Problem D. A Lot of Games
- Codeforces 456 Problem D A Lot of Games
- Codeforces #260(Div 2) D A lot of games(字母树+树形dp)
- CodeForces 456D&455B--A Lot of Games(Trie+博弈)
- Creating Emotion in Games: The Craft and Art of Emotioneering
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 F: qwb has a lot of Coins [nim]【博弈】
- ***qwb has a lot of Coins nim博弈
- CF_148D_BagOfMice