CodeForces 455B.A Lot of Games

B. A Lot of Games

time limit per test 1 second
memory limit per test 256 megabytes

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

Examples
input1

2 3
a
b

output1

First

input2

3 1
a
b
c

output2

First

input3

1 2
ab

output3

Second

Solution

简单的博弈论问题(再加上字典树trie)
对于先手的人,判断出他能否必赢或必输
再对于后手的人,判断出他能否必赢或必输
(关于如何判断,只要树型dp一下)
如果先手的人能必赢和必输,那他可以让自己先输 ( k − 1 ) (k-1) (k1)局,最后一局赢就好
如果先手的人能必赢,但不能必输,那么现在我们就要考虑k的奇偶性,不妨设第一个人是A,第二个人是B,第一轮A先手先胜,第二轮B先手必胜,…,即第 ( 2 n − 1 ) (2n-1) (2n1)轮A胜,第 2 n 2n 2n轮B胜
如果先手的人不能必胜。。。(那他还怎么赢???不可能赢了啊!!!)

Code

#include <cstdio>
#include <string>
#include <iostream>
#define N 100010
#define C 26

using namespace std;
int trie[N][C], f1[N], f2[N], tot;

inline void insert(string s) {
	int len = s.size(), rt = 0;
	for (int i = 0; i < len; ++i) {
		int id = s[i] - 'a';
		if (!trie[rt][id]) trie[rt][id] = ++tot;
		rt = trie[rt][id];
	}
}

void dfs1(int rt) {
	f1[rt] = 1;
	for (int i = 0; i < C; ++i) {
		if (trie[rt][i]) {
			dfs1(trie[rt][i]);
			if (f1[trie[rt][i]]) {
				f1[rt] = 0;
				return;
			}
		}
	}
}

void dfs2(int rt) {
	bool flag = 1;
	for (int i = 0; i < C; ++i) {
		if (trie[rt][i]) {
			dfs2(trie[rt][i]);
			flag = 0;
			if (f2[trie[rt][i]]) {
				f2[rt] = 0;
				return;
			}
		}
	}
	if (flag) f2[rt] = 0;
	else f2[rt] = 1;
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; ++i) {
		string st;
		cin >> st;
		insert(st);
	}
	dfs1(0);//求先手能不能必胜
	dfs2(0);//求先手能不能必输
	if (!f1[0]) {
		if (!f2[0]) printf("First\n");
		else {
			if (k % 2) printf("First\n");
			else printf("Second\n");
		}
	}
	else printf("Second\n");
	return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值