D. A Lot of Games
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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

Sample test(s)
input
2 3
a
b
output
First
input
3 1
a
b
c
output
First
input
1 2
ab
output
Second

题目大意:

给出N个字符。依照规则玩(太长了。这里就不翻译了)。

然后第i-th输了的人,能够在第i+1-th先手,如今要求第k-th赢了的人是谁。

解法:

首先存这些字符。用trie来存,通过trie就非常easy联想到树型DP。这里的DP就不是取最优值之类的了。而是用来弄到达某个节点的胜负情况。

我们首先设节点v,win[v]代表已经组装好的字符刚好匹配到v了。然后须要进行下一步匹配时,先手能否够赢。lose[v]则代表先手是否会输。

叶节点,win[v] = false, lose[v] = true.

其它节点 win[v] = win[v] | !win[child],  lose[v] = lose[v] | !lose[child].  (由于每次赢的人。下一个就不是先手。所以结果肯定是跟下一个节点的赢成对立关系)。

如若win[0] = true , lose[0] = true则意味着第一局的人能够操控结果,否则依照k的次数来推断能否够赢。

代码:

#include <cstdio>
#include <cstring>
#define N_max 123456
#define sigma_size 26 using namespace std; bool win[N_max], lose[N_max];
int n, k;
char st1[N_max]; class Trie{
public:
int ch[N_max][sigma_size];
int sz; Trie() {
sz=0;
memset(ch[0], 0, sizeof(ch[0]));
} int idx(char c) {
return c-'a';
} void insert(char *s) {
int l = strlen(s), u = 0; for (int i = 0; i < l; i++) {
int c = idx(s[i]); if (!ch[u][c]) {
ch[u][c] = ++sz;
memset(ch[sz], 0, sizeof(ch[sz]));
} u = ch[u][c];
}
}
}; Trie T; void init() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%s", st1);
T.insert(st1);
}
} void dfs(int v) {
bool is_leaf = true; win[v] = false;
lose[v] = false; for (int i = 0; i < sigma_size; i++) {
int tmp = T.ch[v][i]; if (tmp) {
is_leaf = false;
dfs(T.ch[v][i]);
win[v] |= !win[T.ch[v][i]];
lose[v] |= !lose[T.ch[v][i]];
}
} if (is_leaf) {
win[v] = false;
lose[v] = true;
}
} void ans(bool res) {
puts(res? "First":"Second");
} void solve() {
dfs(0); if (win[0] && lose[0])
ans(true);
else if (win[0])
ans(k&1);
else
ans(0);
} int main() {
init();
solve();
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

codeforces Round #260(div2) D解决报告的更多相关文章

  1. codeforces Round #259(div2) E解决报告

    E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 meg ...

  2. codeforces Round #259(div2) D解决报告

    D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...

  3. codeforces Round #258(div2) D解题报告

    D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. codeforces Round #259(div2) C解题报告

    C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  5. Codeforces Round #260(div2)C(递推)

    有明显的递推关系: f[i]表示i为数列中最大值时所求结果.num[i]表示数i在数列中出现了几次. 对于数i,要么删i,要么删i-1,只有这两种情况,且子问题还是一样的思路.那么很显然递推一下就行了 ...

  6. codeforces Round #258(div2) C解题报告

    C. Predict Outcome of the Game time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  7. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  8. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  9. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

随机推荐

  1. CrossBridge介绍

    CrossBridge介绍 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs CrossBridge是Adobe FlasCC的开源版本,它提供了一个完整 ...

  2. 关于WHERE后面不能放聚合函数(如SUM(...))的解决办法

    我们在编写SQL语句的时候,常常会遇到需要将SUM()放到WHERE后面作为条件查询,事实证明这样是无法执行的,执行会报异常:聚合不应出现在 WHERE 子句中. 那么如何解决呢,使用HAVING关键 ...

  3. PHP, Python, Node.js 哪个比较适合写爬虫?

    PHP, Python, Node.js 哪个比较适合写爬虫? 1.对页面的解析能力2.对数据库的操作能力(mysql)3.爬取效率4.代码量推荐语言时说明所需类库或者框架,谢谢.比如:python+ ...

  4. Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的

    你是否发现用Texturepacker在打包压缩资源文件之后. 把原稿文件弄丢了,可是又要添加新的小png的时候,却无从下手了,本文就是博主在遇到这个问题后百度了非常多方法,可惜仅仅有plist格式的 ...

  5. tomcat启动Flash退出错误不能被视为解决该错误信息

    tomcat 当有错误 启动startup.bat闪存在退出解决方案 打开 startup.bat 文件 最后 该start 阅读run watermark/2/text/aHR0cDovL2Jsb2 ...

  6. lambda left join .DefaultIfEmpty

    我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘 ...

  7. SQL Server :理解DCM页

    原文:SQL Server :理解DCM页 我们已经讨论了各种不同的页,包括数据页.GAM与SGAM页.PFS页,还有IAM页.今天我们来看下差异变更页(Differential Change Map ...

  8. Three.js 3D打印数据模型文件(.STL)载入中

    3DPrint是现在和未来10年度科技产品的主流之中.广泛的. 对于电子商务类3D打印网站.一个主要功能就是商品3D呈现的方式,那是,3D数据可视化技术. HTML5(WebGL)它可以用于构建3D查 ...

  9. hdu 2191 悼念512四川汶川大地震遇难者——如今宝,感恩生活

    悼念512四川汶川大地震遇难者--如今宝,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  10. 小米2S Mk6.0.1 [只能做测试体验,不能使用]

    上几张高清图片.. 说明: 此版本只能做测试体验,不能做实际使用. 开发者: laser杨万荣 感谢: 秋叶随风ivan, m1cha 及 MoKee Open Source的各位开发者 下载地址:链 ...