Topic links
and reviewed the KMP again. I forgot all about it before.
1#include <bits/stdc++.h>2 using namespacestd;3 #definePB (x) push_back (x)4 #definell Long Long5 #defineMK (x, y) make_pair (x, y)6 #defineLson L, M, rt<<17 #defineMem (a) memset (a, 0, sizeof (a))8 #defineRson m+1, R, rt<<1|19 #defineMem1 (a) memset (a,-1, sizeof (a))Ten #defineMEM2 (a) memset (a, 0x3f, sizeof (a)) One #defineRep (I, A, n) for (int i = A; i<n; i++) A #defineull unsigned long Long -typedef pair<int,int>PLL; - Const DoublePI = ACOs (-1.0); the Const DoubleEPS = 1e-8; - Const intMoD = 1e9+7; - Const intINF =1061109567; - Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} }; + Const intMAXN = 1e6+5; - intNEXT[MAXN], A[MAXN], VIS[MAXN], USED[MAXN]; + CharS[MAXN]; A voidGet_next () { at intLen =strlen (s); - inti =0, j =-1; -next[0] = -1; - while(i<Len) { - if(j==-1|| S[i] = =S[j]) { -i++, J + +; inNext[i] =J; -}Else { toj =Next[j]; + } - } the intpos =Next[len]; * while(POS! =-1) { $Vis[pos] =1;Panax Notoginsengpos =Next[pos]; - } the return ; + } A intMain () the { + intN, M; -Cin>>n>>m; $scanf"%s", s); $ intLen =strlen (s); - Get_next (); - for(inti =1; i<=m; i++) { thescanf"%d", &a[i]); - }WuyiSort (A +1, a+m+1); the intFlag =0; - for(inti =1; i<=m; i++) { Wu if(i = =1|| a[i]-a[i-1]>Len) { - for(intj = A[i]; j<len+a[i]; J + +) { AboutUSED[J] =1; $ } -}Else { - if(vis[len+a[i-1]-A[i]]) { - for(intj = a[i-1]+len; j<len+a[i]; J + +) { AUSED[J] =1; + } the}Else { -Flag =1; $ Break; the } the } the } the if(flag) { -Puts"0"); in return 0; the } thell ans =1; About for(inti =1; i<=n; i++) { the if(!Used[i]) { theAns = (ans* -)%MoD; the } + } -cout<<ans<<Endl; the return 0;Bayi}
Codeforces 535D. Tavas and Malekas KMP