A Lot of Games
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121334#problem/J
Description
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 Input
Input
2 3
a
b
Output
First
Input
3 1
a
b
c
Output
First
Input
1 2
ab
Output
Second
##题意: 给出n个字符串(总长度不超过10^5); A B两人轮流进行如下操作: 1. 初始时目标串为空 2. 当前玩家在目标串末尾添加一个字符 3. 要求每次添加后的目标串是给出的任一字符串的前缀 4. 不能操作者输 (游戏共进行k轮,每轮输者在下一轮为先手)
##题解: 首先这是一个博弈论问题. 要求每次目标串都是给定串的前缀,很自然想到用字典树来建模. 这个游戏有一个特别的地方:本局的赢家在下局是后手. 这意味着玩家可能会选择输掉当前局来获取最后的胜利. 我们只用处理单局的情况: 1. 如果先手在单局中处于必输状态,那么他一定会输掉最后的比赛(一直输). 2. 如果先手在单局中有必胜的走法,那么他不一定会选择在所有情况下都赢. 1. 如果先手可以控制自己必输或必赢,那么他一定会赢得最后的比赛(一直输,最后一场选择胜利). 2. 如果先手只能赢无法输(比如只有一个字符),那么他的结果取决与k的奇偶性(奇胜偶败).
- 综上,只需要处理出单局中先手是必输/必胜/可输可赢,这里采用两次dfs来完成判断:
- 先搜索是否有必胜的走法(叶结点必输,可到必输的点为必胜,只能到必胜的点为必输).
- 再搜索是否有必败的走法(叶结点必输,可到必胜的点为必输,只能到必输的点为必胜).
##代码: ``` cpp #include