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;
    }
  • 相关阅读:
    js 剪切板应用clipboardData
    正则表达式的与或非
    自定义类型转换器
    struts2类库下载
    通过ajax提交form表单
    面试官:为什么Mysql中Innodb的索引结构采取B+树?
    代码生成器:IDEA 强大的 Live Templates
    深入理解JVM,7种垃圾收集器,看完我跪了
    你能说出多线程中sleep、yield、join的用法及sleep与wait区别?
    Java8中一个极其强悍的新特性,很多人没用过(非常实用)
  • 原文地址:https://www.cnblogs.com/oneshot/p/4857123.html
Copyright © 2011-2022 走看看