zoukankan      html  css  js  c++  java
  • Codeforces 455B

    题目链接

    B. A Lot of Games
    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 ≤ 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).

    Sample test(s)
    input
    2 3
    a
    b
    output
    First
    input
    3 1
    a
    b
    c
    output
    First
    input
    1 2
    ab
    output
    Second
    Trie树存储字符串,我们需要保存到达某个结点时是否必胜或者是否存在必败态。
    win[u]表示在结点u是否必胜, lose[u]表示在结点u是否存在必败态。
    Accepted Code:
     1 /*************************************************************************
     2     > File Name: D.cpp
     3     > Author: Stomach_ache
     4     > Mail: sudaweitong@gmail.com
     5     > Created Time: 2014年08月13日 星期三 14时10分37秒
     6     > Propose: 
     7  ************************************************************************/
     8 
     9 #include <cmath>
    10 #include <string>
    11 #include <cstdio>
    12 #include <fstream>
    13 #include <cstring>
    14 #include <iostream>
    15 #include <algorithm>
    16 using namespace std;
    17 /*Let's fight!!!*/
    18 
    19 const int maxn = 100005;
    20 int ch[maxn][30];
    21 
    22 struct Trie {
    23     int sz;
    24     Trie() {
    25           memset(ch[0], 0, sizeof(ch[0]));
    26         sz = 1;
    27     }
    28     void insert(char *s);
    29     void dfs(int u);
    30 };
    31 
    32 inline void Trie::insert(char *s) {
    33       int u = 0, n = (int)strlen(s);
    34     for (int i = 0; i < n; i++) {
    35           int id = s[i] - 'a';
    36         if (!ch[u][id]) {
    37               memset(ch[sz], 0, sizeof(ch[sz]));
    38               ch[u][id] = sz++;
    39         }
    40         u = ch[u][id];
    41     }
    42 }
    43 
    44 bool win[maxn], lose[maxn];
    45 inline void Trie::dfs(int u) {
    46       bool flag = false;
    47       for (int i = 0; i < 30; i++) if (ch[u][i]) {
    48           flag = true;
    49           int v = ch[u][i];
    50           dfs(v);
    51         if (!win[v]) win[u] = true;
    52         if (!lose[v]) lose[u] = true; 
    53     }
    54     if (!flag) lose[u] = true;
    55 }
    56 
    57 char str[maxn];
    58 int main(void) {
    59       int n, k;
    60     Trie A;
    61     scanf("%d %d", &n, &k);
    62     while (n--) scanf("%s", str), A.insert(str);
    63     memset(win, false, sizeof(win));
    64     memset(lose, false, sizeof(lose));
    65     A.dfs(0);
    66     if ((win[0] && lose[0]) || (win[0] && k&1)) puts("First");
    67     else puts("Second");
    68 
    69     return 0;
    70 }
    
    
  • 相关阅读:
    每日一题_191118
    每日一题_191117
    每日一题_191116
    每日一题_191115
    每日一题_191114
    每日一题_191113
    每日一题_191112
    每日一题_191111
    每日一题_191110
    一道抛物线自编题的思考
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3910518.html
Copyright © 2011-2022 走看看