CF 455B(A Lot of Games-树上博弈)


B. 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 3
a
b
output
First
input
3 1
a
b
c
output
First
input
1 2
ab
output
Second

先建字典树,再在树上博弈。

值得一提的是建树和dp的部分都可以写在主函数中

用can_win[i]表示当面对局面i(未走)时,是否用必胜解,can_lose[i]则表示无路可走为赢时的情况

于是可计算出can_win[1]和can_lose[1],其中

CF 455B(A Lot of Games-树上博弈)_第1张图片

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (200000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int a[MAXN][26+1],n=1;
char s[MAXN];
bool can_win[MAXN]={0},can_lose[MAXN]={0};
int main()
{
// 	freopen("d.in","r",stdin);
//	freopen(".out","w",stdout);
	int m,k;
	scanf("%d%d",&m,&k);
	
	For(i,m)
	{
		scanf("%s",s);
		int len=strlen(s);
		int t=1;
		Rep(j,len)
		{
			int p=s[j]-'a';
			if (!a[t][p]) a[t][p]=++n;
			t=a[t][p];
		} 
	}
	ForD(i,n)
	{
		bool b=0;
		Rep(j,26) if (a[i][j]) {b=1;break;}
		if (!b) 
		{
			can_lose[i]=1;
		}
		Rep(j,26) if (a[i][j])
		{
			if (!can_win[a[i][j]]) can_win[i]=1;
			if (!can_lose[a[i][j]]) can_lose[i]=1;
			
		}
	}
	bool b=0;
	if (!can_win[1]) b=0;
	else if (can_lose[1]) b=1;
	else if (k%2) b=1;
	else b=0;
	
	if (b) cout<<"First"<







你可能感兴趣的:(树形DP,博弈论)