Codeforces 455 B. A Lot of Games
[$>Codeforces \space 455 B. A Lot of Games
题目大意 : 有两个人在玩游戏,一共玩 \(k\) 轮,每一轮的一开始有一个空串,双方每一回合需要在空串后面加一个字符,但必须要满足加完这个字符之后的字符串是给定大小为 \(n\) 的母串集合中任意一个串的前缀,不能操作者输,规定第一轮的先手为第一个人,接下来每一轮的先手为上一轮的输家,规定最终的赢家是第 \(k\) 轮的赢家,在双方都采取最优策略的情况下,求最终的赢家是第一轮的先手还是后手.
\(1 \leq n \leq 10^5 \ 1 \leq k \leq 10^9\)
解题思路 :
先单独考虑每一轮的游戏,发现本质上就是对母串建 \(Trie\) 树并在 \(Trie\) 树上每次向下移动,不能移动的输
所以可以先 \(dp\) 出对于 \(Trie\) 树上每一个节点,能获得的最终状态是怎样的.
观察发现,如果一个位置往下走既可以到必胜态又可以必败态,那么就可以通过这个位置控制下一局的先后手
进一步发现,如果某一方既可以必胜又可以必败,那么其必然能获得最终的胜利.
考虑如果子游戏中不存在这样的状态,那么如果先手必败则后手赢,如果先手必胜则胜负由 \(k\) 的奇偶性决定
所以只需要 \(dp\) 记录维护 \(4\) 种值,分别表示 (能必胜,能必败,既能必胜又能必败,什么都不能),枚举后继的状态转移即可
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define N (1000005)
const int win = 1, lose = 2, lover = 3, fucker = 4;
char s[N];
int ch[N][26], f[N], n, k, size;
inline void insert(char *s){
int p = 1, len = strlen(s);
for(int i = 0; i < len; i++){
int c = s[i] - 'a';
if(!ch[p][c]) ch[p][c] = ++size;
p = ch[p][c];
}
}
inline void solve(int u){
f[u] = lose; int cwin = 0, close = 0, all = 0;
for(int c = 0; c < 26; c++) if(ch[u][c]){
int v = ch[u][c]; solve(v), all++;
if(f[v] == lose) cwin = 1;
if(f[v] == win) close = 1;
if(f[v] == fucker) return (void) (f[u] = lover);
}
if(cwin && close) return (void) (f[u] = lover);
if(!cwin && !close && all) return (void) (f[u] = fucker);
if(cwin) f[u] = win; if(close) f[u] = lose;
}
int main(){
size = 1;
read(n), read(k);
for(int i = 1; i <= n; i++) scanf("%s", s), insert(s);
solve(1);
if(f[1] == lover) return puts("First"), 0;
if(f[1] == fucker || f[1] == lose) return puts("Second"), 0;
if(k & 1) puts("First"); else puts("Second");
return 0;
}
Codeforces 455 B. A Lot of Games的更多相关文章
- codeforces水题100道 第十六题 Codeforces Round #164 (Div. 2) A. Games (brute force)
题目链接:http://www.codeforces.com/problemset/problem/268/A题意:足球比赛中如果主场球队的主场球衣和客队的客队球衣颜色一样,那么要求主队穿上他们的可对 ...
- Codeforces Round #164 (Div. 2) A. Games【暴力/模拟/每个球队分主场和客场,所有球队两两之间进行一场比赛,要求双方球服颜色不能相同】
A. Games time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- codeforces 456 D. A Lot of Games(字典数+博弈+思维+树形dp)
题目链接:http://codeforces.com/contest/456/problem/D 题意:给n个字符串.进行k次游戏.每局开始,字符串为空串,然后两人轮流在末尾追加字符,保证新的字符串为 ...
- Codeforces 980 E. The Number Games
\(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...
- CodeForces 451C Predict Outcome of the Game
Predict Outcome of the Game Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- codeforcess水题100道
之所以在codeforces上找这100道水题的原因是为了巩固我对最近学的编程语言的掌握程度. 找的方式在codeforces上的PROBLEMSET中过的题最多的那些题里面出现的最前面的10个题型, ...
- cf451C-Predict Outcome of the Game
http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS ...
- 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 325]B. Stadium and Games
[codeforces 325]B. Stadium and Games 试题描述 Daniel is organizing a football tournament. He has come up ...
随机推荐
- python学习笔记(三)之变量和字符串
在其他语言中,变量就是有名字的存储区,可以将值存储在变量中,也即内存中.在Python中略有不同,python并不是将值存储在变量中,更像是把名字贴在值上边.所以,有些python程序员会说pytho ...
- 子div设置margin-top使得父div也跟着向下移动
之前在写网页的时候,发现一个小问题,就是子div设置margin-top的时候,父的div也会跟着向下移动.我用代码和图描述一下问题: <span style="font-size:1 ...
- 深入理解 JavaScript(四)
前言 Bob 大叔提出并发扬了 S.O.L.I.D 五大原则,用来更好地进行面向对象编程,五大原则分别是: The Single Responsibility Principle(单一职责 SRP) ...
- Jmeter===Jmeter中使用CSV Data Set Config参数化不重复数据执行N遍(转)
Jmeter中使用CSV Data Set Config参数化不重复数据执行N遍 要求: 今天要测试上千条数据,且每条数据要求执行多次,(模拟多用户多次抽奖) 1.用户id有175个,且没有任何排序规 ...
- perl_nc.pl
#!/usr/bin/perl use strict; use IO::Socket; use IO::Select; use Getopt::Std; my %option;getopts('lp: ...
- python之operator操作符函数
operator函数主要分为以下几类:对象比较.逻辑比较.算术运算和序列操作. 举例: #python 3.4 >>> operator.eq(1,2)False >>& ...
- 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...
- HDU 6183 Color it 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题意: 有四种操作: 0:清除所有点 1 x y c : 给点(x, y)添加一种颜色c(颜色不 ...
- centos 6 编译安装php-5.4/5.5(lamp模式)
在安装LAMP架构时,我们常用php-5.3的版本 现进行php-5.4/5.5的编译安装演示: [root@localhost ~]# cd /usr/local/src [root@localho ...
- 手机meta标签(保存下来省的每次都找)
手机网站Meta标签 手机端特有的Meta标签 1.<meta name="viewport" id="viewport" content="w ...