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;
    }
  • 相关阅读:
    Node Sass version 5.0.0 is incompatible with^4.0.0
    解决vue-cli引入sass,报错:this.getResolve is not a function问题
    解决nuxt官方脚手架的一些坑:1、支持es6+语法 2、样式支持sass
    针对【create-nuxt-app新版本v3.2.0】构建项目时没有server配置以及运行后弹出收集匿名数据选项等问题的解决方法
    create-nuxt-app创建出来的目录没有server文件夹
    Redis安装(Windows环境下Redis安装)
    koa2中间件,路由,cookies
    用同步的写法来执行异步操作, async, awiat
    koa2 安装与启动
    练习:自己写一个容器ArrayList集合 一一数组综合练习
  • 原文地址:https://www.cnblogs.com/oneshot/p/4857123.html
Copyright © 2011-2022 走看看