您的位置:首页 > 其它

Codeforces Round #260 (Div. 1) --B. A Lot of Games (Trie)

2015-10-06 14:41 531 查看
B. A Lot of Games

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).

题意:一个游戏,两个人轮流在一个字符串后面添加字符,要求字符串必须是 给定n个字符串的前缀,刚开始字符串是空的,游戏进行k次, 问先手赢还是后手赢。

我们可以先求出两个布尔状态, odd, even, odd表示对于1次游戏先手是否能必赢,even表示先手是否必输。

然后k次游戏,分清况写一下 就出来了。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int M = 27;
char buff[maxn];
struct Trie{
int son[maxn][M], tot, root;
bool odd[maxn * M], even[maxn * M];
void init(){
tot = root = 0;
memset(son, 0, sizeof son);
memset(even, false, sizeof even);
memset(odd, false, sizeof (odd));
}
void insert(char *s){
int cur = root;
for (int i = 0; s[i]; i++){
int ord = s[i] - 'a';
if (!son[cur][ord]){
son[cur][ord] = ++tot;
}
cur = son[cur][ord];
}
}
void dfs(int u){
odd[u] = false;
even[u] = false;
bool leaf = true;
for (int i = 0; i < 26; i++){
int v = son[u][i];
if (!v){
continue;
}
leaf = false;
dfs(v);
even[u] |= !even[v];
odd[u] |= !odd[v];
}
if (leaf){
even[u] = true;
}
}
}tree;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n, k;
while (cin >> n >> k) {
tree.init();
for (int i = 0; i < n; i++) {
scanf ("%s", buff);
tree.insert(buff);
}
tree.dfs(tree.root);
bool odd = tree.odd[tree.root], even = tree.even[tree.root];
if (!odd){
printf("Second\n");
}else if (even){
printf("First\n");
}else{
printf(k&1 ? "First\n" : "Second\n");
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: