Codeforces Round #260 (Div. 1)B题(字典树+DFS+博弈)

B. 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 ≤ 1051 ≤ 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

这题怎么说呢,算是字典树+博弈类型的吧

思路:首先分析一局和只关心先手的输赢情况,用给出的字符串建成一颗字典树,然后在字典树上分别用两次DFS可以求出想赢一定能赢(成功则返回1,否则为0,用变量ans

表示)和想输一定能输(成功输了则返回1,否则为0,用变量ans2表示)的结果

一局的情况搞定了,那么推广到K局呢,题目是要第K次赢的才算赢家,那么现在分ans和ans2的值来讨论

1.ans=1且ans2=1,也就是说对任何一局先手想赢一定能赢,想输也一定能输,那么先手可以在前K-1次选择输,最后一次选择赢,所以这种情况先手一定赢

2.ans=0,这种情况时是不管ans2的值,最后一定是后手赢,因为后手可以让先手一直输

3.ans=1且ans2=0,这要分K的奇偶来讨论,假设现在只讨论K取,1或2,
  
   1.如果K取1,那么对于只有一局的情况,肯定先手赢啦,K推广到3,5,7........也成立
   
   2.如果K取2,那么第一局先手只能赢,那么第二局就轮到后手做先手下棋了,显然后手赢,K推广到4,6,8........也成立

细节看代码啦~~~

#include 
#include 
#include 
#include 
using namespace std;

int n,k;
char s[100010];
int ch[100010][27];
int val[100010];
int sz;
int ans[3];

void insert1()
{
    int u=0;
    int n=strlen(s);
    int i;
    for(i=0;i

你可能感兴趣的:(数据结构_字典树)