博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces Round #260(div2) D解决报告
阅读量:7020 次
发布时间:2019-06-28

本文共 2970 字,大约阅读时间需要 9 分钟。

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 ≤ 1051 ≤ 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 3ab
output
First
input
3 1abc
output
First
input
1 2ab
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 
#include
#define N_max 123456#define sigma_size 26using 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();}

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

你可能感兴趣的文章
iOS开发实用技巧—项目新特性页面的处理
查看>>
activiti自定义流程之Spring整合activiti-modeler5.16实例(三):流程模型列表展示
查看>>
[翻译] ABCIntroView
查看>>
数据的存储介质-固态存储SSD
查看>>
用以替换系统NSLog的YouXianMingLog
查看>>
JVM实用参数(八)GC日志
查看>>
设计模式学习起点 UML类图笔记
查看>>
JS魔法堂:IE5~9的Drag&Drop API
查看>>
Node.js其他模块
查看>>
iOS系统关于URL Schemes的漏洞探究
查看>>
2015年iOS开发者收入调查报告--企业开发者
查看>>
如何在Cocos2D游戏中实现A*寻路算法(七)
查看>>
Linux性能测试 top衍生命令 atop/htop/slaptop
查看>>
Android Animation动画实战(二):从屏幕底部弹出PopupWindow
查看>>
TrueType和Bitmap字体的区别
查看>>
android eclipse写layout文件失效问题解决
查看>>
PHP7扩展开发之hello word
查看>>
SQLite语法
查看>>
AngularJS 实践:应用开发 :: ENA13 价格条码-(最后一里)
查看>>
gnuradio中接收端的信道滤波
查看>>