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
每场游戏从一个空字符串开始,每次添加一个字符保证添加之后得到的字符串至少为给出字符串其中一个的前缀
当前这场游戏输的人下一场先开始
我觉得这个题光是理解就很蛋疼...
看了好多人的答案好像都是用trie建前缀树
ans[i][0] = 1表示选定前缀树中i字符的人会输
ans[i][1] = 1表示选定前缀树中i字符的人会赢
建树完成后从下到上dfs即可
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 100100
#define LL long long
using namespace std;
//ans[i][0] = 1表示当前在i处的人会输
//ans[i][1] = 1表示当前在i处的人会赢
int cnt = 1;
int vis[MAXN][30];
int ans[MAXN][2];
void DFS(int st) {
bool b = 0;
for(int i=0; i<30; ++i) {
if(vis[st][i]) {
b = 1;
int nxt = vis[st][i];
DFS(nxt);
ans[st][0] |= !ans[nxt][0];
ans[st][1] |= !ans[nxt][1];
}
}
ans[st][1] |= !b;
return ;
}
int add(int st, int ch) {
int &cur = vis[st][ch];
return cur ? cur : cur = cnt++;
}
int main(void) {
int n, k, st;
string str;
cin >> n >> k;
while(n--) {
cin >> str;
st = 0;
for(int i=0; i<str.size(); ++i) {
st = add(st, str[i]-'a');
}
}
DFS(0);
if(!ans[0][0] || (!ans[0][1]&&k%2==0))
cout << "Second" << endl;
else cout << "First" << endl;
return 0;
}