牛客15334 Easygoing Single Tune Circulation

链接

点击跳转

题解

如果答案要想是 y e s yes 的话,第一种情况就是:它是原串重复两次所形成字符串的子串。这个很简单,把每个原串重复两次扔进广义后缀自动机,到时候直接查找即可。

如果上述情况不满足,还想输出 y e s yes 的话,那么查询串一定是由原串重复两次及以上(最后一段可能不完整)得到的,这个形式很容易联想到 k m p kmp 的一些经典用法。我们用 k m p kmp 找到这个循环节,注意这样找出的肯定是最小循环节。原串也整成最小循环节的形式,插入字典树,查询的时候直接上字典树查询就行了,注意二者都要使用最小表示法。

代码

#include <bits/stdc++.h>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 200010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct KMP
{
    int next[maxn], t[maxn];
    void build(char *r, int len)
    {
        int i, j=0;
        for(i=1;i<=len;i++)t[i]=r[i];  t[len+1]=0;
        for(i=2;i<=len;i++)
        {
            for(;j and t[j+1]!=t[i];j=next[j]);
            next[i] = t[j+1]==t[i]?++j:0;
        }
    }
    int move(int pos, int x)
    {
        for(;pos and t[pos+1]!=x;pos=next[pos]);
        return t[pos+1]==x ? pos+1 : 0;
    }
}kmp;
struct Trie
{
    int tot, ch[maxn][26], end[maxn];
    void init(){while(tot--)cl(ch[tot+1]),end[tot+1]=0;tot=1;}
    int* operator[](int index){return ch[index];}
    int id(char c){return c-'a';}
    int append(int pos, int c)
    {
        int &t=ch[pos][c];
        return t?t:t=++tot;
    }
    int insert(char* s, int n)
    {
        int pos=1, i;
        for(i=1;i<=n;i++)pos=append(pos,id(s[i]));
        end[pos]++;
        return pos;
    }
}trie, trie_sam;
struct EXSAM
{
    int tot, ch[maxn<<1][26], fa[maxn<<1], len[maxn<<1], pref[maxn<<1];
    int* operator[](int u){return ch[u];}
    void init()
    {
        int i;
        rep(i,tot)cl(ch[i]),fa[i]=len[i]=pref[i]=0;
        tot=1;
    }
    int append(int las, int c)
    {
        int p(las);
        len[las=++tot]=len[p]+1;
        pref[las]=1;
        for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;
        if(!p)fa[las]=1;
        else
        {
            int q=ch[p][c];
            if(len[q]==len[p]+1)fa[las]=q;
            else
            {
                int qq=++tot;
                memcpy(ch[qq],ch[q],sizeof(ch[q]));
                fa[qq]=fa[q];
                len[qq]=len[p]+1;
                fa[q]=fa[las]=qq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
            }
        }
        return las;
    }
    void bfs_build(Trie& trie)
    {
        init();
        queue<pii> q;
        q.em(pii(1,1));
        while(!q.empty())
        {
            auto pr=q.front(); q.pop();
            int pos=pr.first, u=pr.second;
            for(int i=0;i<26;i++)
                if(trie[pos][i])
                {
                    int to=trie[pos][i], v=ch[u][i]?ch[u][i]:append(u,i);
                    q.em(pii(to,v));
                }
        }
    }
    int mov(int p, int c){return max(1,ch[p][c]);}
}exsam;
struct minimal_cyclic_expression
{
    int w[maxn<<1];
    int run(char* s, int n)
    {
        int i=1, j=2, k;
        rep(k,n)w[k]=w[n+k]=s[k];
        while(i<=n and j<=n)
        {
            for(k=0;k<n and w[i+k]==w[j+k];k++);
            if(k==n)return i;
            if(w[i+k]<w[j+k])j = j+k+1==i ? i+1 : j+k+1;
            else i = i+k+1==j ? j+1 : i+k+1;
        }
        return min(i,j);
    }
}mce;
char s[maxn];
ll n, T, q;
int main()
{
    ll i, kase;
    T=read();
    rep(kase,T)
    {
        printf("Case #%lld:\n",kase);
        trie.init();
        trie_sam.init();
        exsam.init();
        n=read();
        while(n--)
        {
            scanf("%s",s+1);
            auto len=strlen(s+1);
            rep(i,len)s[i+len]=s[i];
            trie_sam.insert(s,len*2);
            kmp.build(s,len);
            ll c = len-kmp.next[len];
            if(len%c!=0)c=len;
            ll pos=mce.run(s,c);
            trie.insert(s+pos-1,c);
            rep(i,len)s[i]=0; s[len+1]=0;
        }
        exsam.bfs_build(trie_sam);
        q=read();
        while(q--)
        {
            scanf("%s",s+1);
            auto len=strlen(s+1);
            ll p=1;
            rep(i,len){p=exsam.mov(p,s[i]-'a');if(p==1)break;}
            if(i>len){printf("YES\n");continue;}
            kmp.build(s,len);
            ll c = len-kmp.next[len];
            ll pos = mce.run(s,c);
            p=1;rep(i,c)p=trie[p][mce.w[pos+i-1]-'a'];
            if(trie.end[p])printf("YES\n");
            else printf("NO\n");
            rep(i,len)s[i]=0; s[len+1]=0;
        }
    }
    return 0;
}
发布了863 篇原创文章 · 获赞 72 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/103860085