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

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
题意:有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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值