B. A Lot of Games
time limit per test
memory limit per test
input
output
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players.
n
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
n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109).
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
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],其中
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
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<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;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"<<endl;
else cout<<"Second"<<endl;
return 0;
}