传送门
文章目录
- 题意:
- 思路:
题意:
思路:
由kmpkmpkmp中失配数组nenene的含义我们知道,ne[i],ne[ne[i]],...ne[i],ne[ne[i]],...ne[i],ne[ne[i]],...都是iii的相等的前后缀,但是可能有重叠的部分,那么就有一个显然的做法:对于每个iii,不断向前跳,记录ne[x]<=i/2ne[x]<=i/2ne[x]<=i/2的个数,复杂度O(n2)O(n^2)O(n2)。
考虑优化,我们记录一个数组cnt[i]cnt[i]cnt[i]表示可重叠的后缀个数,这个显然可以通过求nenene的时候递推出来,那么我们跳到ne[x]<=i/2ne[x]<=i/2ne[x]<=i/2的时候直接加上cnt[x]cnt[x]cnt[x]即可,但是这样还是会被aaaaaaaaaaaaaaa这种的串串卡掉,继续优化。
考虑利用之前的信息,由于到了iii我们就暴跳到ne[x]<=i/2ne[x]<=i/2ne[x]<=i/2,那么对于i+1i+1i+1一定有ne[x]<=(i+1)/2ne[x]<=(i+1)/2ne[x]<=(i+1)/2,满足要求,复杂度O(n)O(n)O(n)。
当然还有一个无脑的做法,就是倍增优化暴跳的方式,复杂度O(tnlogn)O(tnlogn)O(tnlogn),能过也是奇迹,不过还是需要一些卡常的,比如将数组f[N][20]f[N][20]f[N][20]写成f[20][N]f[20][N]f[20][N],这样快了1s1s1s。
O(n)O(n)O(n)
// Problem: P2375 [NOI2014] 动物园
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2375
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
char s[N];
int ne[N],pre[N];inline int read(){char ch=getchar(); int x=0,w=1;while(ch<'0'||ch>'9') {if(ch=='-') w=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48); ch=getchar();}return x*w;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; _=read();while(_--) {scanf("%s",s+1);n=strlen(s+1);pre[1]=1;for(int i=2;i<=n;++i) {int j=ne[i-1];while(j&&s[i]!=s[j+1]) j=ne[j];if(s[i]==s[j+1]) j++;ne[i]=j; pre[i]=pre[j]+1;}LL ans=1;for(int i=2,j=0;i<=n;i++) {while(j&&s[i]!=s[j+1]) j=ne[j];if(s[i]==s[j+1]) j++;while(j>i/2) j=ne[j];ans*=pre[j]+1; ans%=mod;}printf("%lld\n",ans);}return 0;
}
/*
abababab
*/
O(tnlogn)O(tnlogn)O(tnlogn)
// Problem: P2375 [NOI2014] 动物园
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2375
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
char s[N];
int ne[N];
int f[21][N];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf("%d",&_);while(_--) {scanf("%s",s+1);n=strlen(s+1);for(int i=2,j=0;i<=n;i++) {while(j&&s[i]!=s[j+1]) j=ne[j];if(s[i]==s[j+1]) j++;ne[i]=j; f[0][i]=ne[i];}for(int k=1;k<=19;k++) for(int i=1;i<=n;i++) f[k][i]=f[k-1][f[k-1][i]];int ans=1;for(int i=2;i<=n;i++) {int now=0,x=i;for(int j=19;j>=0;j--) if(f[j][x]*2>i) x=f[j][x];for(int j=19;j>=0;j--) if(f[j][x]) now+=1<<j,x=f[j][x];ans=1ll*ans*(now+1)%mod;}printf("%d\n",ans);}return 0;
}
/**/