洛谷 P2375 [NOI2014]动物园

目录:


题目:

传送门


分析:

前缀和后缀互不相交,其实就是说 n e x t [ i ] 必须 [ i / 2 ] ,( [ ] 表示向下取整)。
那么就先求出一个正常的 n e x t 数组,然后再多增加一个循环,与求 n e x t 数组很像,只不过多了一行,保证 n e x t 数组只有 i 的一半:
w h i l e ( j 2 > i + 1 ) j = n e x t [ j ]
然后就求出 s u m 的值即可。


代码:

// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define LL unsigned long long
#define maxl 1000000+1
#define mod 1000000007
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
int n,f[maxl],t[maxl];
string s;
int main() {
    n=read();
    while(n--) {
        getline(cin,s);
        f[0]=-1;
        f[1]=0;
        t[0]=0;
        t[1]=1;
        int j=0;
        LL ans=1ull;
        for(int i=1;i<(int)s.length();i++) {
            while(j>=0&&s[i]!=s[j]) j=f[j];
            f[i+1]=++j;
            t[i+1]=t[j]+1;
        }
        j=0;
        for(int i=1;i<(int)s.length();i++) {
            while(j>=0&&s[i]!=s[j]) j=f[j];
            j++;
            while((j<<1)>(i+1)) j=f[j];
            ans*=(LL)t[j]+1;
            ans%=mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35786326/article/details/81783318