[LOJ6377]「是男人就过8题——Pony.ai」AStringGame

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值