Sequence I
problem DescriptionMr. Frog has sequencesa1,a2,?,an andb1,b2,?,bm and a number p. He wants to know the number of positions q such that sequenceb1,b2,?,bm is exactly the sequenceAQ,AQ+P,Aq +2 P,? aq+ (m< Span id= "mathjax-span-79" class= "Mo" >−1 ) p where < Span id= "mathjax-span-84" class= "Mrow" >q + ( M−1 ) p ≤n and q ≥1 .
InputThe first line contains only one integerT≤ , which indicates the number of test cases.
Each test case contains three lines.
The first line contains three space-separated integers1≤n≤6,1≤m≤6 and1≤p≤6 .
The second line contains n integersA1,a2,?,an(1≤ai≤9) .
The third line contains m integersB1,b2,?,bM(1≤bi≤9) .
OutputFor each test case, output one line ' case #x: Y ', where x is the case number (starting from 1) and Y is the number of Vali D Q ' s.
Sample Input26 3 11 2 3 1 2 31 2 36 3 21 3 2 2 3 11 2 3
Sample OutputCase #1:2Case #2:1
Test instructions
Give you a A, a two sequence
And a p, ask how many Q exactly satisfies b1,b2,b3....bm is a[q],a[q+p],a[q+2p]......a[q+ (m-1) p];
Exercises
A sequence of a, each P-position is divided into a group, so that there is a maximum of P group, the number and n
Run each group and B sequence KMP calculate the answer
#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF =1e18;Const DoublePi = ACOs (-1.0);Const intN = 1e6+Ten, M = 1e6, mod = 1e9+7, INF =2e9;intT,n,m,p,s[n],t[n],ans =0; Vector<int>P[n];intNex[n];intMain () {intCAS =1; scanf ("%d",&T); while(t--) {scanf ("%d%d%d",&n,&m,&p); for(inti =1; I <= N; ++i) scanf ("%d",&T[i]); for(inti =1; I <= m; ++i) scanf ("%d",&S[i]); for(inti =0; I < P; ++i) p[i].clear (); memset (NEX,0,sizeof(NEX)); Ans=0; intK =0; for(intI=2; i<=m; i++) { while(k>0&&s[k+1]!=s[i]) K =Nex[k]; if(s[k+1]==s[i]) k++; Nex[i]=K; } if(p = =1) {k=0; for(intI=1; i<=n; i++) { while(k>0&&s[k+1]!=t[i]) K =Nex[k]; if(s[k+1]==t[i]) k++; if(k==m) {k=Nex[k]; Ans++; }} printf ("Case #%d:", cas++); printf ("%d\n", ans); } Else { for(inti =1; I <= N; ++i) p[i%P].push_back (T[i]); for(inti =0; I < P; ++i) {k=0; for(intj =0; J < P[i].size (); ++j) { while(k>0&&s[k+1]!=P[I][J]) K =Nex[k]; if(s[k+1]==P[I][J]) k++; if(k==m) {k=Nex[k]; Ans++; }}} printf ("Case #%d:", cas++); printf ("%d\n", ans); } }}
HDU 5918 Sequence I KMP