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$,使得集合中的每 ...
 
随机推荐
- 【转】 KVC/KVO原理详解及编程指南
			
原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的 ...
 - 使用charles proxy for Mac来抓取手机App的网络包
			
之前做Web项目的时候,经常会使用Fiddler(Windows下).Charles Proxy(Mac下)来抓包,调试一些东西:现在搞Android App开发,有时候也需要分析手机App的网络请求 ...
 - 不用Google Adsense的84个赚钱方法
			
这是一个关于网络广告商和网络销售的汇总列表,可以用来为您的网站或博客赚点钱.广告商都是英文的,加入广告请确认其是否支持中国地区支持,不支持的话就不必加入了. Chitika : 购物中心旗帜广告. ( ...
 - Delphi ControlCount和ComponentCount的区别
			
ComponentCount指打开的窗体所拥有的控件个数,包含所有子组件.孙组件(子组件内的子组件) 如上图,Form1的ComponentCount是13,而Panel1的ComponentCoun ...
 - 115个Java面试题和答案——终极列表
			
from http://www.importnew.com/10980.html#collection http://www.importnew.com/11028.html 下面的章节分为上下两篇, ...
 - Oracle 面试宝典 - General Questions
			
转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...
 - Django Admin 简单部署上线
			
前言 打算为公司弄一个管理公用密码的平台,由于比较懒,就选择使用Django admin,默认的admin并不漂亮,于是我使用了这个django-suit插件来美化 如图: 是不是比原来的漂亮多了. ...
 - UVa1605 - Building for UN(构造法)
			
UVA - 1605 Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Description ...
 - const 笔记
			
.指向const的指针例如:double a=1.01;const double * b=&a;*b=2.1; //这显然是错误的a=2.1; //这是正确的,a和*b的值都会变成2.01,有 ...
 - 2013 年 —— Facebook 在开源方面的工作介绍
			
自从 Facebook 的第一行PHP代码,第一句 MySQL 的 INSERT 语句,开源就已经是我们工程哲学中的一个重要的部分. 现在,我们使用.维护并为大量的主要项目做出了贡献——涉及多种领域如 ...