CF 455B-- A Lot of Games

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 3
a
b
output
First
input
3 1
a
b
c
output
First
input
1 2
ab
output
Second
思路:构造字典树,自底向上递推一下。fuck[x][0]为1表示踩在节点x上能让自己输。fuck[x][1]为1表示踩在节点x上能让自己赢
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
#define inf 0x3f3f3f3f
#define maxn 100080
#define LL long long int
char str[maxn];
struct Node
{
	int id,dep;
	bool operator < (const Node & a)const
	{
		return dep < a.dep;
	}
}node[maxn];
struct Trie
{
	int first[maxn],ch[maxn],nxt[maxn],fuck[maxn][2];
	int cnt;
	void init()
	{
		memset(fuck,0,sizeof(fuck));
		cnt = 1;
		first[0] = -1;
	}
	int idx(char c)
	{
		return c - 'a';
	}
	void insert(char * s)
	{
		int u = 0,j;
		int len = strlen(s);
		for(int i = 0;i < len;i++)
		{
			int id = idx(s[i]);
			bool flag = false;
			for(j = first[u];j != -1;j = nxt[j])
			{
				if(ch[j] == id)
				{
					flag = true;
					break;
				}
			}
			if(flag)	u = j;
			else 
			{
				node[cnt].id = cnt;
				node[cnt].dep = i+1;
				ch[cnt] = id;
				first[cnt] = -1;
				nxt[cnt] = first[u];
				first[u] = cnt++;
				u = cnt-1;
			}
		}
	}
	void gao()
	{
		sort(node+1,node+cnt);
		for(int i = cnt-1;i >= 1;i--)//能让自己赢
		{
			bool flag = true;
			for(int j = first[node[i].id];j != -1;j = nxt[j])
			{
				if(fuck[j][1])
				{
					flag = false;
					break;
				}
			}
			fuck[node[i].id][1] = flag;
		}
		for(int i = cnt-1;i >= 1;i--)//踩了这一步能让自己输吗
		{
			bool flag = true;
			for(int j = first[node[i].id];j != -1;j = nxt[j])
			{
				if(fuck[j][0])
				{
					flag = false;
					break;
				}
			}
			fuck[node[i].id][0] = flag;
			if(first[node[i].id] == -1)	fuck[node[i].id][0] = 0;
		}
		for(int i = first[0];i != -1;i = nxt[i])
		{
			if(fuck[i][0])	fuck[0][0] = 1;
			if(fuck[i][1])	fuck[0][1] = 1;
		}
	}
}trie;
int main()
{
	//freopen("in.txt","r",stdin);
	int n,k;
	while(scanf("%d%d",&n,&k)==2)
	{
		trie.init();
		for(int i = 1;i <= n;i++)
		{
			scanf("%s",str);
			trie.insert(str);
		}
		trie.gao();
		if(trie.fuck[0][0] && trie.fuck[0][1])	cout << "First" << endl;
		else if(trie.fuck[0][0])
		{
			cout << "Second" << endl;
		}
		else if(trie.fuck[0][1])
		{
			if(k&1)	cout << "First" << endl;
			else cout << "Second" << endl;
		}
		else cout << "Second" << endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值