• Codeforces Round #260 (Div. 2) D


    D. 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 ≤ 1051 ≤ 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

     题意:给出一些字符串,要求每次两个人轮流取字符放在一个新串的后面,使得新串是已知字符串集合中的前缀,第k次胜利的则最后胜利。 判断胜负情况。

    sl :很久就见过这个题目,今天终于算是补上了(又是一道老提) 。 树形dp一下,记录到当前节点的胜负情况。

    注意:由于做trie树时设置了一个虚拟节点0 所以递归到叶子节点的时候胜负有所颠倒。

    然后得到0节点的胜负情况。如果win[0]=true&&false =true;也就是当前节点可胜可负,完全取决第一个人,所以这种情况第一个人胜利。

    然后就是win[0]=true.  false[0]=false ; 也就是谁先取谁胜。 这取决游戏的次数。

    注意特判1次的情况。剩下的就很简单了。

      1 //by caonima

     2 //hehe
     3 #include <bits/stdc++.h>
     4 using namespace std;
     5 const int MAX = 1e5+10;
     6 int win[MAX],lose[MAX],G[MAX][30];
     7 char str[MAX];
     8 int n,k,cur;
     9 void insert(char *S) {
    10     int root=0;
    11     for(int i=0;S[i];i++) {
    12         int x=S[i]-'a';
    13         if(!G[root][x]) {
    14             G[root][x]=++cur;
    15         }
    16         root=G[root][x];
    17     }
    18 }
    19 void dfs(int u) {
    20     int is_left=true,v;
    21     win[u]=lose[u]=false;
    22     for(int i=0;i<=26;i++) {
    23         if(v=G[u][i]) {
    24             dfs(v); is_left=false;
    25             win[u]|=!win[v];
    26             lose[u]|=!lose[v];
    27         }
    28     }
    29     if(is_left) lose[u]=true;
    30 }
    31 void gao() {
    32     dfs(0);
    33    // printf("%d %d",win[0],lose[0]);
    34     if(k==1) {
    35         win[0] ? printf("First ") : printf("Second ");
    36     }
    37     else if(win[0]&&lose[0]) {
    38         printf("First ");
    39     }
    40     else if(win[0]) {
    41         (k&1) ? printf("First ") : printf("Second ");
    42 
    43     }
    44     else {
    45         printf("Second ");
    46     }
    47 }
    48 
    49 int main() {
    50     while(scanf("%d %d",&n,&k)==2) {
    51         cur=0;
    52         for(int i=1;i<=n;i++) {
    53             scanf("%s",str);
    54             insert(str);
    55         }
    56         gao() ;
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    Android获取View对应的Bitmap
    Android按需添加Google Play服务
    Android自定义View的构造函数
    两个Fragment之间如何传递数据
    SmartImageView
    onSingleTapUp()和onSingleTapConfirmed()的区别
    Android Fragment add/replace以及backstack
    InputStream与InputStreamReader的区别
    Android手动签名
    使用后台服务数据更新UI
  • 原文地址:https://www.cnblogs.com/acvc/p/3902970.html
Copyright © 2020-2023  润新知