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
       
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值