Codeforces Round #260 (Div. 2) D
1 second
256 megabytes
standard input
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.
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.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
2 3
a
b
First
3 1
a
b
c
First
1 2
ab
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的更多相关文章
- 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) ...
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...
- Codeforces Round #260 (Div. 2)AB
http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...
- 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 ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- 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 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- Codeforces Round #260 (Div. 2)
A. Laptops 题目意思: 给定n台电脑,第i台电脑的价格是ai ,质量是bi ,问是否存在一台电脑价格比某台电脑价格底,但质量确比某台电脑的质量高,即是否存在ai < aj 且 bi & ...
- 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 ...
随机推荐
- 240 Search a 2D Matrix II 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...
- maven idea
写在前面的话:此篇文章教程是在IntelliJ IDEA中搭建的maven项目.(建议eclipse党快点转IDEA吧,IDEA大法好.逃… 1.maven的安装 前往Apache Maven官网点击 ...
- 06使用NanoPiM1Plus在Android4.4.2下接U盘
06使用NanoPiM1Plus在Android4.4.2下接U盘 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本:V ...
- 联想 P70-t 免解锁BL 免rec Magisk Xposed 救砖 ROOT
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- 【C++】智能指针简述(四):shared_ptr
在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...
- Slow HTTP Denial of Service Attack 漏洞解决办法
编辑 删除 问题名称: Slow HTTP Denial of Service Attack 问题URL http://10.238.*.*:58*** 风险等级: 高 问题类型: 服务器配置类 漏洞 ...
- vue2.0路由(跳转和传参)经典介绍
声明式 <router-link :to="...">编程式router.push(...) router.push('home') / ...
- [Windows Server 2012] 初识Windows Server 2012
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:初次见识Win ...
- Verilog之event
1 Explicit event The value changes on nets and variable can be used as events to trigger the executi ...
- Xcode 6 Beta 高速官方下载地址
推荐迅雷下载: http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/xcode_6_beta.dmg