bzoj3670: [Noi2014]动物园

题意:求a[1:i]的2*|border|<=i的num+1乘积
题解:建kmp自动机(即next[i]指向i),由于某个点到根就是a[1:i]的border,然后树上二分即可

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+10,maxn=1000000+10,inf=0x3f3f3f3f;

vi v[N];
char s[N];
int fail[N],st[N],top;
void getfail()
{
    fail[0]=-1;
    int k=-1,n=strlen(s);
    for(int i=0;i<=n;i++)v[i].clear();
    v[0].pb(1);
    for(int i=1;i<n;i++)
    {
        while(k>-1&&s[k+1]!=s[i])k=fail[k];
        if(s[k+1]==s[i])k++;
        fail[i]=k;
        v[fail[i]+1].pb(i+1);
    }
}
ll ans;
void dfs(int u)
{
    if(u)st[++top]=u;
    int l=0,r=top;
    while(l<r-1)
    {
        int m=(l+r)>>1;
        if(st[m]*2<=u)l=m;
        else r=m;
    }
//    printf("%d %d\n",l,u);
    ans=ans*(l+1)%mod;
    for(int i=0;i<v[u].size();i++)dfs(v[u][i]);
    if(u)--top;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        getfail();
        top=0;ans=1;dfs(0);
        printf("%lld\n",ans);
    }
    return 0;
}
/********************

********************/
posted @ 2019-04-29 18:52  walfy  阅读(121)  评论(0编辑  收藏  举报