Description
John's n (1 ≤ n ≤ 100000) cows have K (1 ≤ k ≤ 25000) bad guys who love trouble. when the cows queue in a certain order, these bad guys will always stand together. to find out the bad guys, John arranged his cows into the cowshed and needed your eye to identify the bad guys. to distinguish them, John issued a number card for all the cows, it says 1 .. number between S (1 ≤ S ≤ 25. although this is not a perfect method, it can also play a role. john does not remember the numbers of the bad guys. however, based on his memory, he gave a "pattern string ". if the numbers of the original bad guys are the same, their numbers in the pattern strings are still the same. the relationship between the numbers of bad guys in the pattern string is the same as that of the original number. for example, for such a pattern string. The first six bad guys are the same as the last number (although not necessarily 1), and their numbers are the smallest in the gang. they have the same number (not necessarily 4) and are the largest of the bad guys. now all the cows are in a queue with numbers such as 5, 6, 10, 7, 3, 2, and 9. The substrings are 2, 10, 7, 3, and 2, meeting the same relationship and size relationship of the pattern strings, so this is the bad guys. Please find out all the possibilities of the bad guys. input
Input three integers n, k, S. In the next n rows, enter a cow number in each row. In the next K rows, enter a pattern string number in each row. Output
Row 1st outputs an integer B. Next row B has an integer in each row, indicating the starting position of a possible bad guy gang.
Question:
Code:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring> //by zrt//problem:using namespace std;typedef long long LL;const int inf(0x3f3f3f3f);const double eps(1e-9);int n,k,s;int aa[100005][26],bb[100005][26];int a[100005],b[100005];bool cmp(int j,int i,int c1,int c2){//b 0...j | a i-j..i int sum1=0,sum2=0; for(int k=1;k<c1;k++){ sum1+=j?bb[j-1][k]:0; } for(int k=1;k<c2;k++){ sum2+=(i?aa[i-1][k]:0)-((i-j)?aa[i-j-1][k]:0); } return sum1==sum2&&(j?bb[j-1][c1]:0)==((i?aa[i-1][c2]:0)-((i-j)?aa[i-j-1][c2]:0));}bool cmpp(int j,int i,int c1,int c2){//b 0...j |b i-j..i int sum1=0,sum2=0; for(int k=1;k<c1;k++){ sum1+=j?bb[j-1][k]:0; } for(int k=1;k<c2;k++){ sum2+=(i?bb[i-1][k]:0)-((i-j)?bb[i-j-1][k]:0); } return sum1==sum2&&(j?bb[j-1][c1]:0)==((i?bb[i-1][c2]:0)-((i-j)?bb[i-j-1][c2]:0));}int nxt[100005]; void mk_pre(){ for(int i=0;i<n;i++) { if(i)for(int j=1;j<=s;j++) aa[i][j]=aa[i-1][j]; aa[i][a[i]]++; } for(int i=0;i<k;i++) { if(i)for(int j=1;j<=s;j++) bb[i][j]=bb[i-1][j]; bb[i][b[i]]++; }}void mk(){ int j=-1; nxt[0]=-1; for(int i=1;i<k;i++){ while(~j&&!cmpp(j+1,i,b[j+1],b[i])) j=nxt[j]; if(cmpp(j+1,i,b[j+1],b[i])) j++; nxt[i]=j; }} int ans[100005],top;int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif scanf("%d%d%d",&n,&k,&s); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=0;i<k;i++){ scanf("%d",&b[i]); } mk_pre();// qianzhuihe mk(); int j=-1; for(int i=0;i<n;i++){ while(~j&&!cmp(j+1,i,b[j+1],a[i])) j=nxt[j]; if(cmp(j+1,i,b[j+1],a[i])) j++; if(j==k-1){ ans[top++]=i-j+1; j=nxt[j]; } } printf("%d\n",top); for(int i=0;i<top;i++){ printf("%d\n",ans[i]); } return 0;}
Bzoj 1729: [usaco DEC] cow patterns mode matching