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 + ;
const int M = ;
char buff[maxn];
struct Trie{
int son[maxn][M], tot, root;
bool odd[maxn * M], even[maxn * M];
void init(){
tot = root = ;
memset(son, , sizeof son);
memset(even, false, sizeof even);
memset(odd, false, sizeof (odd));
}
void insert(char *s){
int cur = root;
for (int i = ; 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 = ; i < ; 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 = ; 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\n");
}else if (even){
printf("First\n");
}else{
printf(k& ? "First\n" : "Second\n");
} }
return ;
}

Codeforces Round #260 (Div. 1) --B. A Lot of Games (Trie)的更多相关文章

  1. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  2. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  4. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  5. Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)

    链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a ...

  6. Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

    题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...

  7. Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)

    题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...

  8. Codeforces Round #369 (Div. 2) B. Chris and Magic Square (暴力)

    Chris and Magic Square 题目链接: http://codeforces.com/contest/711/problem/B Description ZS the Coder an ...

  9. Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)

    题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...

随机推荐

  1. CSS3渐变(Gradients)-线性渐变

    CSS3渐变(Gradients)可以让你在两个或多个指定颜色之间显示平稳的过度,包括透明度. 以前,你必须使用图像来实现这些效果.但是,通过Css3渐变(Gradients),你可以减少下载的事件和 ...

  2. CSS3新增Hsl、Hsla、Rgba色彩模式以及透明属性(转)

    CSS2中色彩模式只有RGB色彩模式(RGB即RED.Green.BLue)和十六进制(Hex)模式,为了能支持 透明opacity 的Alpha值,CSS3又增加了RGBA色彩模式(RGBA即RED ...

  3. c# try..... catch

    功能说明:在此例中,try 块包含对可能导致异常的ProcessString()方法的调用.catch子句包含仅在屏幕上显示消息的异常处理程序,当从ProcessString内部调用throw语句时, ...

  4. String or binary data would be truncated. The statement has been terminated.

    常见的情况为:插入的值大于字段定义的最大长度. String or binary data would be truncated. The statement has been terminated

  5. SQL语句之三简单增删改查

    这是前面建的库和表 USE Test go INSERT dbo.MyTable --插入数据         ( NAME ,age) VALUES  ( '数据,20  -- NAME - var ...

  6. Struts2 单个文件上传/多文件上传

    1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war 单个文件上传 upload.jsp <s:form action= ...

  7. angular的post提交

    用下来明显感觉jquery的post提交比ng的post提交好用很多 一开始,用angularjs的$http提交的数据,在php服务器端无法通过 因为jQuery会把作为JSON对象的data序列化 ...

  8. python - StringIO文本缓冲

    参考:http://pymotwcn.readthedocs.org/en/latest/documents/StringIO.html 类StringIO提供了一个在内存中方便处理文本的类文件(读, ...

  9. 安卓AVD使用建议

    问题描述:之前在安装了Android开发环境后,一开始并没有直接在Android手机和平板上进行调试,是使用的AVD模拟器工具.由于电脑的配置不是特别好,总感觉AVD的使用速度太慢,包括启动的时候还有 ...

  10. github克隆项目中的子模块submodule时遇到的问题

    GitHub真是个开源大宝库,不只能学习代码,还能学习git的使用! 最近在研究Off-the-Record-iOS项目(https://github.com/chrisballinger/Off-t ...