codeforces Round #260(div2) D解题报告_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:55:46
原创
943人浏览过

猫眼课题宝
猫眼课题宝

5分钟定创新选题,3步生成高质量标书!

猫眼课题宝 85
查看详情 猫眼课题宝

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 3ab
登录后复制

output

First
登录后复制

input

3 1abc
登录后复制

output

First
登录后复制

input

1 2ab
登录后复制

output

Second
登录后复制

题目大意:

给出N个字符,按照规则玩(太长了,这里就不翻译了)。然后第i-th输了的人,可以在第i+1-th先手,现在要求第k-th赢了的人是谁。

解法:

首先存这些字符,用trie来存,通过trie就很容易联想到树型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 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();}
登录后复制

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号