zoukankan      html  css  js  c++  java
  • Codeforces Round #260 (Div. 1) --B. A Lot of Games (Trie)

    B. A Lot of Games
     

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

    题意:一个游戏,两个人轮流在一个字符串后面添加字符,要求字符串必须是 给定n个字符串的前缀,刚开始字符串是空的,游戏进行k次, 问先手赢还是后手赢。

    我们可以先求出两个布尔状态, odd, even, odd表示对于1次游戏先手是否能必赢,even表示先手是否必输。

    然后k次游戏,分清况写一下 就出来了。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5 + 5;
    const int M = 27;
    char buff[maxn];
    struct Trie{
        int son[maxn][M], tot, root;
        bool odd[maxn * M], even[maxn * M];
        void init(){
            tot = root = 0;
            memset(son, 0, sizeof son);
            memset(even, false, sizeof even);
            memset(odd, false, sizeof (odd));
        }
        void insert(char *s){
            int cur = root;
            for (int i = 0; s[i]; i++){
                int ord = s[i] - 'a';
                if (!son[cur][ord]){
                    son[cur][ord] = ++tot;
                }
                cur = son[cur][ord];
            }
        }
        void dfs(int u){
            odd[u] = false;
            even[u] = false;
            bool leaf = true;
            for (int i = 0; i < 26; i++){
                int v = son[u][i];
                if (!v){
                    continue;
                }
                leaf = false;
                dfs(v);
                even[u] |= !even[v];
                odd[u] |= !odd[v];
            }
            if (leaf){
                even[u] = true;
            }
        }
    }tree;
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif
        int n, k;
        while (cin >> n >> k) {
            tree.init();
            for (int i = 0; i < n; i++) {
                scanf ("%s", buff);
                tree.insert(buff);
            }
            tree.dfs(tree.root);
            bool odd = tree.odd[tree.root], even = tree.even[tree.root];
            if (!odd){
                printf("Second
    ");
            }else if (even){
                printf("First
    ");
            }else{
                printf(k&1 ? "First
    " : "Second
    ");
            }
    
        }
        return 0;
    }
  • 相关阅读:
    List of yellow pages
    Bing Beats Google for the Best Way to X-Ray Search LinkedIn
    Search Engine Hacking – Manual and Automation
    42 Bing Search Engine Hacks
    Bing Advanced Search Tricks You Should Know
    Advanced search keywords
    10 Advanced Bing Search Tricks You Should Know
    python判断list中是否包含某个元素
    Python性能优化的20条建议
    CheatEngine查看PE header
  • 原文地址:https://www.cnblogs.com/oneshot/p/4857123.html
Copyright © 2011-2022 走看看