BZOJ 3670 NOI2014 动物园
2017-03-13 14:06
211 查看
这题有点坑啊.
题解:
首先根据KMP的next数组可以构造出一棵树,可以看出,题目要求的是num是一个点往上走的第一个长度小于i/2的节点(设为ti)的深度.
于是可以先求每个节点的深度
再脑补出一个倍增+二分的做法.
然后就T得很惨.(1e6的nlog2n,极限数据4s).
再观察,发现我们要求的ti,由于i/2单调不降,所以ti在这条链上的位置也是单调不降的.
然后就可以像next数组那样,每次记录上个ti的位置,然后像普通KMP那样利用迭代求解.
题解:
首先根据KMP的next数组可以构造出一棵树,可以看出,题目要求的是num是一个点往上走的第一个长度小于i/2的节点(设为ti)的深度.
于是可以先求每个节点的深度
再脑补出一个倍增+二分的做法.
然后就T得很惨.(1e6的nlog2n,极限数据4s).
再观察,发现我们要求的ti,由于i/2单调不降,所以ti在这条链上的位置也是单调不降的.
然后就可以像next数组那样,每次记录上个ti的位置,然后像普通KMP那样利用迭代求解.
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctime> #include<string> #include<iomanip> #include<algorithm> #include<map> using namespace std; #define LL long long #define FILE "dealing" #define up(i,j,n) for(int i=j;i<=n;++i) #define db double #define uint unsigned int #define eps 1e-12 #define pii pair<int,int> int read(){ int x=0,f=1,ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return f*x; } const int maxn=1000010,maxm=20000,limit=1e6,mod=(int)(7+1e9+0.1); const db inf=(1e18); template<class T>bool cmax(T& a,T b){return a<b?a=b,true:false;} template<class T>bool cmin(T& a,T b){return a>b?a=b,true:false;} template<class T>T min(T& a,T& b){return a<b?a:b;} template<class T>T max(T& a,T& b){return a>b?a:b;} char s[maxn]; int n,next[maxn]; int num[maxn],t[maxn]; int main(){ freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); int T=read(); while(T--){ scanf("%s",s+1); n=strlen(s+1); LL ans=1,j=0; t[1]=1; up(i,2,n){ while(j&&s[j+1]!=s[i])j=next[j]; if(s[j+1]==s[i])j++; next[i]=j; t[i]=t[j]+1; } j=1; up(i,2,n){ while(j&&s[j+1]!=s[i])j=next[j]; if(s[j+1]==s[i])j++; while((j<<1)>i)j=next[j]; ans=ans*(t[j]+1)%mod; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- bzoj 3670: [Noi2014]动物园 kmp与扩展kmp
- BZOJ3670【NOI2014】动物园(KMP变形)
- 【KMP,next树】BZOJ3670 [Noi2014]动物园
- bzoj 3670: [Noi2014]动物园
- 【BZOJ3670】【NOI2014】动物园 KMP变形 线性出解
- [BZOJ3670][Noi2014]动物园 && KMP
- 【BZOJ3670】【UOJ5】【NOI2014】动物园
- bzoj 3670: [Noi2014]动物园
- BZOJ3670: [Noi2014]动物园
- 【KMP】BZOJ3670-[Noi2014]动物园
- [BZOJ3670][NOI2014]动物园(KMP)
- [bzoj3670] [Noi2014]动物园
- BZOJ3670 & 洛谷2375 & UOJ5:[NOI2014]动物园——题解
- BZOJ_3670_[NOI2014]_动物园_(kmp)
- bzoj 3670: [Noi2014]动物园
- BZOJ_3670_[Noi2014]动物园_KMP
- BZOJ 3670 [Noi2014]动物园 (KMP)
- BZOJ 3670: [Noi2014]动物园 KMP题解
- BZOJ 3670: [Noi2014]动物园 KMP
- [BZOJ3670][Noi2014]动物园(KMP)