[$>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的更多相关文章

  1. codeforces水题100道 第十六题 Codeforces Round #164 (Div. 2) A. Games (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/268/A题意:足球比赛中如果主场球队的主场球衣和客队的客队球衣颜色一样,那么要求主队穿上他们的可对 ...

  2. 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 ...

  3. codeforces 456 D. A Lot of Games(字典数+博弈+思维+树形dp)

    题目链接:http://codeforces.com/contest/456/problem/D 题意:给n个字符串.进行k次游戏.每局开始,字符串为空串,然后两人轮流在末尾追加字符,保证新的字符串为 ...

  4. Codeforces 980 E. The Number Games

    \(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...

  5. CodeForces 451C Predict Outcome of the Game

    Predict Outcome of the Game Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  6. codeforcess水题100道

    之所以在codeforces上找这100道水题的原因是为了巩固我对最近学的编程语言的掌握程度. 找的方式在codeforces上的PROBLEMSET中过的题最多的那些题里面出现的最前面的10个题型, ...

  7. cf451C-Predict Outcome of the Game

    http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS     ...

  8. 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 ...

  9. [codeforces 325]B. Stadium and Games

    [codeforces 325]B. Stadium and Games 试题描述 Daniel is organizing a football tournament. He has come up ...

随机推荐

  1. UIAlertView---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址   UIAlertView   //转载请注明出处--本文永久链接:http://ww ...

  2. Bower A package manager for the web

    Bower can manage components that contain HTML, CSS, JavaScript, fonts or even image files. Bower doe ...

  3. linux进程管理-定时定期执行任务

     0.计划任务的命令: at 安排作业在某一时刻执行 batch 安排作业在系统负载不重时执行 crontab 安排周期性运行的作业 1.at命令用法: 安排命令或者多个命令在指定的时间运行一次 语法 ...

  4. LCD实验学习笔记(七):NAND FLASH

    s3c2440 CPU内置NAND FLASH控制器.相关寄存大器起始地址为0x4e000000. 通过设置NFCONF寄存器,设置NAND FLASH 时序. 通过设置NFCONT寄存器,使能NAN ...

  5. wce.exe getpass.exe 读取密码

    http://www.ampliasecurity.com/research/wce_v1_4beta_x32.zip http://www.ampliasecurity.com/research/w ...

  6. Win7蓝屏代码0X0000007B可能是SATA mode问题

    Win7蓝屏代码0X0000007B可能是硬盘模式的问题,我进入BIOS把SATA的mode从Enhanced改为Compatible(及IDE兼容模式)结果系统可以顺利启动没有问题.       从 ...

  7. tableView选中行的调用顺序/ 取消选中Cell

    UITableViewCell它有两个属性highLighted.selected.很明显一个是高亮状态, 一个是选中状态. UITableViewCell, 对应的2个方法 // 高亮状态调用的方法 ...

  8. tomcat8特性

    作者:Eilen,转载需注明.博客主页:http://www.cnblogs.com/Eilen/ 一.Apache Tomcat 8介绍 Apache Tomcat 8RC1版于前几日发布.它 经过 ...

  9. FineReport——巧妙实现类tab布局

    在FR中,表达form支持局部刷新和tab布局,在报表中,不能做到这样,只能舍弃一些功能来做到类似的tab布局. 首先,在参数面板放一个文本控件temp,用作一个临时值,需要设置一个默认值,而切换是通 ...

  10. 22:django 配置详解

    django配置文件包含了你的django安装的所有配置信息,本节为大家详细讲解django的配置 基本知识 一个配置文件只是一个包含模块级别变量的的python模块,所有的配置变量都是大写的,哈哈哈 ...