您的位置:首页 > 其它

CF455B- A Lot of Games(Trie树+DFS+博弈)

2014-08-12 13:50 190 查看
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个字符串的前缀。上一盘输的人下一盘会变成先手,问最后谁赢?

思路:先建立Trie树,考虑3中情况:1、后手能必胜:那么后手可以保证一直是后手,且一直赢,那么胜者就是后手。 2、先手能必胜,且能必输:那么先手可以保证一直输,然后在最后一盘的时候选择必胜。 3、先手能必胜,但不不能必输:那么每次先手只能胜,而且每次比赛先手后手会调换位置,因此只要看K的奇偶性就行。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
int sz;
int trie[maxn][26];
int n,k;
int getIdx(char a){
return a-'a';
}
void insert(string st){
int u = 0;
for(int i = 0; i < st.size(); i++){
int k = getIdx(st[i]);
if(!trie[u][k]){
trie[u][k] = sz;
memset(trie[sz],0,sizeof trie[sz]);
sz++;
}
u = trie[u][k];
}
}
void init(){
sz = 1;
memset(trie[0],0,sizeof trie[0]);
}

bool dfs1(int id){
for(int i = 0; i < 26; i++){
if(trie[id][i]!=0){
if(!dfs1(trie[id][i])){
return 1;
}
}
}
return 0;
}
bool dfs2(int id){
int ans = 0,flag = 1;
for(int i = 0; i < 26; i++){
if(trie[id][i] != 0){
flag = 0;
if(!dfs2(trie[id][i]))
return 1;
}
}
if(flag) return 1;
return 0;
}

int main(){

while(~scanf("%d%d",&n,&k)){
string tstr;
init();
for(int i = 0; i < n; i++){
cin >> tstr;
insert(tstr);
}
int a = dfs1(0),b = dfs2(0);
if(a==0){
printf("Second\n");
}
else if(a==1&&b==1){
printf("First\n");
}
else if(a==1&&b==0){
if(k&1){
printf("First\n");
}else{
printf("Second\n");
}
}
}
return 0;
}


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