Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp
題目鏈接:
http://codeforces.com/problemset/problem/535/D
D. Tavas and Malekas
time limit per test2 secondsmemory limit per test256 megabytes
問題描述
Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string s of length n comes out from Tavas' mouth instead.
Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on s. Malekas has a favorite string p. He determined all positions x1?<?x2?<?...?<?xk where p matches s. More formally, for each xi (1?≤?i?≤?k) he condition sxisxi?+?1... sxi?+?|p|?-?1?=?p is fullfilled.
Then Malekas wrote down one of subsequences of x1,?x2,?... xk (possibly, he didn't write anything) on a piece of paper. Here a sequence b is a subsequence of sequence a if and only if we can turn a into b by removing some of its elements (maybe no one of them or all).
After Tavas woke up, Malekas told him everything. He couldn't remember string s, but he knew that both p and s only contains lowercase English letters and also he had the subsequence he had written on that piece of paper.
Tavas wonders, what is the number of possible values of s? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him.
Answer can be very large, so Tavas wants you to print the answer modulo 109?+?7.
輸入
The first line contains two integers n and m, the length of s and the length of the subsequence Malekas wrote down (1?≤?n?≤?106 and 0?≤?m?≤?n?-?|p|?+?1).
The second line contains string p (1?≤?|p|?≤?n).
The next line contains m space separated integers y1,?y2,?...,?ym, Malekas' subsequence (1?≤?y1?<?y2?<?...?<?ym?≤?n?-?|p|?+?1).
輸出
In a single line print the answer modulo 1000?000?007.
樣例輸入
6 2
ioi
1 3
樣例輸出
26
題意
給你一個子串p,且在長度為n的原串中出現(xiàn)的m個插入位,問原串有幾種可能。
題解
我們只要考慮插入位置之間的重疊部分是否能夠完全匹配,這個其實就是p串的前綴在和后綴匹配,用kmp處理出failed指針就可以輕松解決了。
代碼
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<ctime> #include<vector> #include<cstdio> #include<string> #include<bitset> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> using namespace std; #define X first #define Y second #define mkp make_pair #define lson (o<<1) #define rson ((o<<1)|1) #define mid (l+(r-l)/2) #define sz() size() #define pb(v) push_back(v) #define all(o) (o).begin(),(o).end() #define clr(a,v) memset(a,v,sizeof(a)) #define bug(a) cout<<#a<<" = "<<a<<endl #define rep(i,a,b) for(int i=a;i<(b);i++) #define scf scanf #define prf printftypedef __int64 LL; typedef vector<int> VI; typedef pair<int,int> PII; typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f; const LL INFL=0x3f3f3f3f3f3f3f3fLL; const double eps=1e-8; const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=1e6+10;const int mod=1e9+7;LL xp26[maxn];char str[maxn]; int arr[maxn]; int n,m,len;int f[maxn],vis[maxn]; void getFail(char* P,int* f){int m=strlen(P);f[0]=0; f[1]=0;for(int i=1;i<m;i++){int j=f[i];while(j&&P[i]!=P[j]) j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}clr(vis,0);int j=f[m];while(j){vis[j]=1; // bug(j);j=f[j];} }int main() {scf("%d%d",&n,&m);scf("%s",str);len=strlen(str);getFail(str,f);rep(i,0,m) scf("%d",&arr[i]);int ans=m?len:0;for(int i=1; i<m; i++) {int x1=arr[i-1],x2=arr[i];int y1=x1+len-1,y2=x2+len-1;if(y1<x2) ans+=len;else {int l=y1-x2+1; // bug(l);if(vis[l]) {ans+=y2-y1;} else {prf("0\n");return 0;}}}LL last=1;for(int i=0;i<n-ans;i++) last*=26,last%=mod;prf("%I64d\n",last);return 0; }//end-----------------------------------------------------------------------寫了個哈希的,死且僅死在第67組數(shù)據(jù),xrz,
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<ctime> #include<vector> #include<cstdio> #include<string> #include<bitset> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> using namespace std; #define X first #define Y second #define mkp make_pair #define lson (o<<1) #define rson ((o<<1)|1) #define mid (l+(r-l)/2) #define sz() size() #define pb(v) push_back(v) #define all(o) (o).begin(),(o).end() #define clr(a,v) memset(a,v,sizeof(a)) #define bug(a) cout<<#a<<" = "<<a<<endl #define rep(i,a,b) for(int i=a;i<(b);i++) #define scf scanf #define prf printftypedef __int64 LL; typedef vector<int> VI; typedef pair<int,int> PII; typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f; const LL INFL=0x3f3f3f3f3f3f3f3fLL; const double eps=1e-8; const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=1e6+10;const int P=321; const int mod=1e9+7;unsigned long long H[maxn],xp[maxn]; LL xp26[maxn];char str[maxn]; int arr[maxn]; int n,m,len;void pre() {xp[0]=1;rep(i,1,maxn) xp[i]=xp[i-1]*P;xp26[0]=1;rep(i,1,maxn) xp26[i]=xp26[i-1]*26%mod; }void get_H() {H[0]=0;for(int i=1; i<=len; i++) {H[i]=H[i-1]*P+str[i]-'a'+1;} }unsigned long long query(int l,int r) {return H[r]-H[l-1]*xp[r-l+1]; }int main() {pre();scf("%d%d",&n,&m);scf("%s",str+1);len=strlen(str+1);get_H();rep(i,0,m) scf("%d",&arr[i]);sort(arr,arr+m);int ans=m?len:0;for(int i=1; i<m; i++) {int x1=arr[i-1],x2=arr[i];int y1=x1+len-1,y2=x2+len-1;if(y1<x2) ans+=len;else {int l=y1-x2+1;if(query(1,l)==query(len-l+1,len)) {ans+=y2-y1;} else { // puts("fuck"); // bug(l);prf("0\n");return 0;}}} // bug(ans);prf("%I64d\n",xp26[n-ans]);return 0; }//end-----------------------------------------------------------------------轉(zhuǎn)載于:https://www.cnblogs.com/fenice/p/5902276.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利尔达蓝牙模组LSD4BT-E66ALS
- 下一篇: ViewStub