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.
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.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
2 3 a b
First
3 1 a b c
First
1 2 ab
Second
思路:构造字典树,自底向上递推一下。fuck[x][0]为1表示踩在节点x上能让自己输。fuck[x][1]为1表示踩在节点x上能让自己赢
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #include <cmath> using namespace std; #define inf 0x3f3f3f3f #define maxn 100080 #define LL long long int char str[maxn]; struct Node { int id,dep; bool operator < (const Node & a)const { return dep < a.dep; } }node[maxn]; struct Trie { int first[maxn],ch[maxn],nxt[maxn],fuck[maxn][2]; int cnt; void init() { memset(fuck,0,sizeof(fuck)); cnt = 1; first[0] = -1; } int idx(char c) { return c - 'a'; } void insert(char * s) { int u = 0,j; int len = strlen(s); for(int i = 0;i < len;i++) { int id = idx(s[i]); bool flag = false; for(j = first[u];j != -1;j = nxt[j]) { if(ch[j] == id) { flag = true; break; } } if(flag) u = j; else { node[cnt].id = cnt; node[cnt].dep = i+1; ch[cnt] = id; first[cnt] = -1; nxt[cnt] = first[u]; first[u] = cnt++; u = cnt-1; } } } void gao() { sort(node+1,node+cnt); for(int i = cnt-1;i >= 1;i--)//能让自己赢 { bool flag = true; for(int j = first[node[i].id];j != -1;j = nxt[j]) { if(fuck[j][1]) { flag = false; break; } } fuck[node[i].id][1] = flag; } for(int i = cnt-1;i >= 1;i--)//踩了这一步能让自己输吗 { bool flag = true; for(int j = first[node[i].id];j != -1;j = nxt[j]) { if(fuck[j][0]) { flag = false; break; } } fuck[node[i].id][0] = flag; if(first[node[i].id] == -1) fuck[node[i].id][0] = 0; } for(int i = first[0];i != -1;i = nxt[i]) { if(fuck[i][0]) fuck[0][0] = 1; if(fuck[i][1]) fuck[0][1] = 1; } } }trie; int main() { //freopen("in.txt","r",stdin); int n,k; while(scanf("%d%d",&n,&k)==2) { trie.init(); for(int i = 1;i <= n;i++) { scanf("%s",str); trie.insert(str); } trie.gao(); if(trie.fuck[0][0] && trie.fuck[0][1]) cout << "First" << endl; else if(trie.fuck[0][0]) { cout << "Second" << endl; } else if(trie.fuck[0][1]) { if(k&1) cout << "First" << endl; else cout << "Second" << endl; } else cout << "Second" << endl; } }