P2375 [NOI2014] 动物园 kmp fail指针/倍增
傳送門
文章目錄
- 題意:
- 思路:
題意:
思路:
由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; } /**/總結
以上是生活随笔為你收集整理的P2375 [NOI2014] 动物园 kmp fail指针/倍增的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 剪头发圆方三角是怎么回事(剪头发什么是方
- 下一篇: 怎么看蜘蛛爬行的记录(怎么看蜘蛛爬行的记