Description
最近 Alice 和 Bob 在玩一个和字符串有关的游戏。在游戏开始之前,他们会准备 n个字符串 s1~sn
和一个模板串 t, 保证这 n 个字符串都是 t 的子串。
游戏开始后,他们会轮流地执行以下操作,由 Alice 先手。
从 n 个字符串中选择一个字符串 si
在 si 末尾增加一个字符;
得到的新字符串需要是 t 的子串;
如果上述过程无法完成,当前玩家失败,假设 Alice 和 Bob 都以最优策略行动,求出谁是游戏的胜者。
|t|<=1e5,∑|si|<=3e7
Solution
好了,我也是1/8个男人了(做男人好难不如女装)
签到题没什么好说的,直接建SAM,在上面求SG就好了
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=2e5+5;
int n,lst,tot,ty,sg[N],p[N],vis[N],ws[N],son[N][26],pr[N],len[N];
char s[N];
int extend(int p,int x) {
int np=++tot;len[np]=len[p]+1;
for(;p&&!son[p][x];p=pr[p]) son[p][x]=np;
if (!p) pr[np]=1;
else {
int q=son[p][x];
if (len[q]==len[p]+1) pr[np]=q;
else {
int nq=++tot;
fo(j,0,25) son[nq][j]=son[q][j];
pr[nq]=pr[q];len[nq]=len[p]+1;
pr[q]=pr[np]=nq;
for(;p&&son[p][x]==q;p=pr[p]) son[p][x]=nq;
}
}
return np;
}
int main() {
while(scanf("%s",s+1)!=EOF) {
n=strlen(s+1);
fo(i,1,tot) {
pr[i]=len[i]=0;
fo(j,0,25) son[i][j]=0;
}
tot=lst=1;
fo(i,1,n) lst=extend(lst,s[i]-'a');
fo(i,1,n) ws[i]=0;
fo(i,1,tot) ws[len[i]]++;
fo(i,1,n) ws[i]+=ws[i-1];
fd(i,tot,1) p[ws[len[i]]--]=i;
fo(i,1,tot) vis[i]=0;
fd(i,tot,1) {
int x=p[i];
fo(j,0,25) if (son[x][j]) vis[sg[son[x][j]]]=x;
for(sg[x]=0;vis[sg[x]]==x;sg[x]++);
}
int nim=0;
for(scanf("%d",&ty);ty;ty--) {
scanf("%s",s+1);int m=strlen(s+1);
int x=1;
fo(i,1,m) x=son[x][s[i]-'a'];
nim^=sg[x];
}
if (nim) puts("Alice");
else puts("Bob");
}
return 0;
}