CodeForces 455B.A Lot of Games

版权声明:蒟蒻的博文,dalao转载标明出处就好吖 https://blog.csdn.net/jokingcoder/article/details/81452741

B. A Lot of Games

time limit per test 1 second

memory limit per test 256 megabytes

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).

Examples
input1

2 3
a
b

output1

First

input2

3 1
a
b
c

output2

First

input3

1 2
ab

output3

Second

Solution

简单的博弈论问题(再加上字典树trie)
对于先手的人,判断出他能否必赢或必输
再对于后手的人,判断出他能否必赢或必输
(关于如何判断,只要树型dp一下)
如果先手的人能必赢和必输,那他可以让自己先输 ( k 1 ) 局,最后一局赢就好
如果先手的人能必赢,但不能必输,那么现在我们就要考虑k的奇偶性,不妨设第一个人是A,第二个人是B,第一轮A先手先胜,第二轮B先手必胜,…,即第 ( 2 n 1 ) 轮A胜,第 2 n 轮B胜
如果先手的人不能必胜。。。(那他还怎么赢???不可能赢了啊!!!)

Code

#include <cstdio>
#include <string>
#include <iostream>
#define N 100010
#define C 26

using namespace std;
int trie[N][C], f1[N], f2[N], tot;

inline void insert(string s) {
    int len = s.size(), rt = 0;
    for (int i = 0; i < len; ++i) {
        int id = s[i] - 'a';
        if (!trie[rt][id]) trie[rt][id] = ++tot;
        rt = trie[rt][id];
    }
}

void dfs1(int rt) {
    f1[rt] = 1;
    for (int i = 0; i < C; ++i) {
        if (trie[rt][i]) {
            dfs1(trie[rt][i]);
            if (f1[trie[rt][i]]) {
                f1[rt] = 0;
                return;
            }
        }
    }
}

void dfs2(int rt) {
    bool flag = 1;
    for (int i = 0; i < C; ++i) {
        if (trie[rt][i]) {
            dfs2(trie[rt][i]);
            flag = 0;
            if (f2[trie[rt][i]]) {
                f2[rt] = 0;
                return;
            }
        }
    }
    if (flag) f2[rt] = 0;
    else f2[rt] = 1;
}

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) {
        string st;
        cin >> st;
        insert(st);
    }
    dfs1(0);//求先手能不能必胜
    dfs2(0);//求先手能不能必输
    if (!f1[0]) {
        if (!f2[0]) printf("First\n");
        else {
            if (k % 2) printf("First\n");
            else printf("Second\n");
        }
    }
    else printf("Second\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jokingcoder/article/details/81452741
今日推荐