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 ≤ 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

题意:给出一些字符串,要求每次两个人轮流取字符放在一个新串的后面,使得新串是已知字符串集合中的前缀,第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+;
 6 int win[MAX],lose[MAX],G[MAX][];
 7 char str[MAX];
 8 int n,k,cur;
 9 void insert(char *S) {
     int root=;
     for(int i=;S[i];i++) {
         int x=S[i]-'a';
         if(!G[root][x]) {
             G[root][x]=++cur;
         }
         root=G[root][x];
     }
 }
 void dfs(int u) {
     int is_left=true,v;
     win[u]=lose[u]=false;
     for(int i=;i<=;i++) {
         if(v=G[u][i]) {
             dfs(v); is_left=false;
             win[u]|=!win[v];
             lose[u]|=!lose[v];
         }
     }
     if(is_left) lose[u]=true;
 }
 void gao() {
     dfs();
    // printf("%d %d",win[0],lose[0]);
     if(k==) {
         win[] ? printf("First\n") : printf("Second\n");
     }
     else if(win[]&&lose[]) {
         printf("First\n");
     }
     else if(win[]) {
         (k&) ? printf("First\n") : printf("Second\n");
 
     }
     else {
         printf("Second\n");
     }
 }
 
 int main() {
     while(scanf("%d %d",&n,&k)==) {
         cur=;
         for(int i=;i<=n;i++) {
             scanf("%s",str);
             insert(str);
         }
         gao() ;
     }
     return ;
 }

Codeforces Round #260 (Div. 2) D的更多相关文章

  1. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  2. 递推DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

  3. Codeforces Round #260 (Div. 2)AB

    http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...

  4. Codeforces Round #260 (Div. 1) D. Serega and Fun 分块

    D. Serega and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/pro ...

  5. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  6. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  7. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  8. Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)

    题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...

  9. Codeforces Round #260 (Div. 2)

    A. Laptops 题目意思: 给定n台电脑,第i台电脑的价格是ai ,质量是bi ,问是否存在一台电脑价格比某台电脑价格底,但质量确比某台电脑的质量高,即是否存在ai < aj 且 bi & ...

  10. Codeforces Round #260 (Div. 2) C

    Description Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. On ...

随机推荐

  1. 爬虫—Selenium使用

    Selenium使用 Selenium是一个自动化测试工具,可以驱动浏览器器执行特定的动作,如点击,下拉等.同时还可以获取浏览器当前呈现页面的源代码,可见即可爬. 1.准备 我们使用谷歌Chrome浏 ...

  2. JS中定时器的返回数值ID值

    定时器会返回一个数字值id,可以由clearInterval(id)或clearTimeout(id)来实现对对应定时器的清除. setInterval()/setTimeout()BOM中的Wind ...

  3. (数论)51NOD 1135 原根

    设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根.(其中φ(m)表示m的欧拉函数)   给出1个质数P,找出P最小的原根. Input 输入1个质数P(3 <= P &l ...

  4. 21 调试我们的C#程序

    我们写的C#程序,很多的时候不是能一次就写对的.尤其是在实际的项目中,你经常要做的工作就是不断修改你写的程序,让它正常运行.程序有错误,主要是两种情况.一种是程序报错了,不能正常运行下去.另一种是程序 ...

  5. SqlServer学习-常用的sql语句-持续更新中

    1.获取数据库下的所有表名 select TABLE_NAME from information_schema.tables where TABLE_TYPE='Base TABLE' 2.随机取出1 ...

  6. springboot与dubbo整合入门(三种方式)

    Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...

  7. [ POI 2010 ] Antisymmetry

    \(\\\) \(Description\) 给出一个长度为 \(N\) 的二进制串,定义一个子串是优秀的,当且仅当其正着看,和倒着按位取反后看结果是一样的,求整个串有多少个优秀的子串. \(N\le ...

  8. python做一个数独小游戏

    最近看了下python的一些知识,在这里记载一下. 1.首先是安装,在官网下载最新的版本3.6,安装的时候要注意在下面勾选上ADD TO PATH,安装的时候会自动写入到环境变量里面,如果没有勾选,可 ...

  9. interface与抽象类

    抽象类: 定义:在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类 抽象类不能被实例化. 抽象类被继承之后,子类必 ...

  10. 关于 . H 宏定义技巧

    #ifndef   LABEL #define   LABEL //代码部分 #endif LABEL为一个唯一的标号,命名规则跟变量的命名规则一样.常根据它所在的头文件名来命名,例如,如果头文件的文 ...