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 + ;
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)的更多相关文章
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- 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 ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)
题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- 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 ...
- Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...
- Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)
题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...
- 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 ...
- Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)
题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...
随机推荐
- 为Activity设置特定权限才能启动
1.在AndroidManifest文件中,声明一个权限,并在activity中添加属性 <!--声明权限,权限名一般为包名+permission+类名 --> <permissio ...
- LINQ to SQL 运行时动态构建查询条件
在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...
- 数据库导出excel表数据
- 执行之前 (错误) 消息 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 OLE DB 错误.错误代码: 0x80040E37. (S ...
- Linux下inotify监控文件夹状态,发生变化后触发rsync同步
1.安装工具--inotifywget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar ...
- [转]Delphi执行CMD命令
今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...
- javascript——面向对象程序设计(2)
<script type="text/javascript"> //1.理解原型对象 //2.原型与in操作符 //3.更简单的原型语法 //4.原型的动态性 //5. ...
- 用连接池提高Servlet访问数据库的效率
Java Servlet作为首选的服务器端数据处理技术,正在迅速取代CGI脚本.Servlet超越CGI的优势之一在于,不仅多个请求可以共享公用资源,而且还可以在不同用户请求之间保留持续数据.本文介绍 ...
- ios开发之代理&&协议
Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替.Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法.也就是说,它只告诉你要做什么,但具体怎么做 ...
- jquery动画效果中,避免持续反应用户的连续点击
一.某些动画效果中,避免持续连续反应用户的连续点击(这标题真不好描述) 意思就是指用户点击速度很快,完成一次效果的时间不能很快结束的话,就会出现用户不点击了,效果还在持续.看下面例子就明白了,手风琴效 ...
- JQuery焦点Table
;;} .table-bordered{;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;} .table{mar ...