Cow Patterns
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2610 | Accepted: 972 |
Description
A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows likes to make trouble. When placed in a line, these troublemakers stand together in a particular order. In order to locate these troublemakers, FJ has lined up his N (1 <= N <= 100,000) cows. The cows will file past FJ into the barn, staying in order. FJ needs your help to locate suspicious blocks of K cows within this line that might potentially be the troublemaking cows.
FJ distinguishes his cows by the number of spots 1..S on each cow's coat (1 <= S <= 25). While not a perfect method, it serves his purposes. FJ does not remember the exact number of spots on each cow in the subgroup of troublemakers. He can, however, remember which cows in the group have the same number of spots, and which of any pair of cows has more spots (if the spot counts differ). He describes such a pattern with a sequence of K ranks in the range 1..S. For example, consider this sequence:
If the true count of spots for some sequence of cows is:
Please help FJ locate all the length-K subsequences in his line of cows that match his specified pattern.
FJ distinguishes his cows by the number of spots 1..S on each cow's coat (1 <= S <= 25). While not a perfect method, it serves his purposes. FJ does not remember the exact number of spots on each cow in the subgroup of troublemakers. He can, however, remember which cows in the group have the same number of spots, and which of any pair of cows has more spots (if the spot counts differ). He describes such a pattern with a sequence of K ranks in the range 1..S. For example, consider this sequence:
1 4 4 3 2 1In this example, FJ is seeking a consecutive sequence of 6 cows from among his N cows in a line. Cows #1 and #6 in this sequence have the same number of spots (although this number is not necessarily 1) and they have the smallest number of spots of cows #1..#6 (since they are labeled as '1'). Cow #5 has the second-smallest number of spots, different from all the other cows #1..#6. Cows #2 and #3 have the same number of spots, and this number is the largest of all cows #1..#6.
If the true count of spots for some sequence of cows is:
5 6 2 10 10 7 3 2 9then only the subsequence 2 10 10 7 3 2 matches FJ's pattern above.
Please help FJ locate all the length-K subsequences in his line of cows that match his specified pattern.
Input
Line 1: Three space-separated integers: N, K, and S
Lines 2..N+1: Line i+1 describes the number of spots on cow i.
Lines N+2..N+K+1: Line i+N+1 describes pattern-rank slot i.
Lines 2..N+1: Line i+1 describes the number of spots on cow i.
Lines N+2..N+K+1: Line i+N+1 describes pattern-rank slot i.
Output
Line 1: The number of indices, B, at which the pattern matches
Lines 2..B+1: An index (in the range 1..N) of the starting location where the pattern matches.
Lines 2..B+1: An index (in the range 1..N) of the starting location where the pattern matches.
Sample Input
9 6 10 5 6 2 10 10 7 3 2 9 1 4 4 3 2 1
Sample Output
1 3
Hint
Explanation of the sample:
The sample input corresponds to the example given in the problem statement.
There is only one match, at position 3 within FJ's sequence of N cows.
The sample input corresponds to the example given in the problem statement.
There is only one match, at position 3 within FJ's sequence of N cows.
Source
题解:找出大小关系一样的串
题解:用KMP....纠结了好久,有事不写了....
#include<stdio.h>
#include<string.h>
int n,k,s;
int a[100005],b[25005],next[25005];
int c1[25005],c2[25005],tree[30],que[105005];
int low_bit(int x)
{
return x&(-x);
}
int sum(int x)
{
int temp=0;
while(x>0)
{
temp+=tree[x];
x-=low_bit(x);
}
return temp;
}
void updata(int x,int y)
{
while(x<=s)
{
tree[x]+=y;
x+=low_bit(x);
}
}
void get_next()
{
int i=1,j,l;
j=next[1]=0;
while(i<=k)
{
if(j==0||(sum(b[i]-1)==c1[j]&&sum(b[i])==c2[j]))
{
next[++i]=++j;
if(i<=k) updata(b[i],1);
}
else
{
for(l=i-j+1;l<=i-next[j];l++) updata(b[l],-1);
j=next[j];
}
}
}
int kmp()
{
int res=0,i=1,j=1,l;
updata(a[1],1);
while(i<=n)
{
if(j==0||(sum(a[i]-1)==c1[j]&&sum(a[i])==c2[j]))
{
i++,j++;
if(i<=n) updata(a[i],1);
}
else
{
for(l=i-j+1;l<=i-next[j];l++) updata(a[l],-1);
j=next[j];
}
if(j>k)
{
for(l=i-j+1;l<=i-next[j];l++) updata(a[l],-1);
que[res++]=i-k;
j=next[j];
}
}
return res;
}
int main()
{
int i,res;
scanf("%d%d%d",&n,&k,&s);
for(i=1;i<=n;i++) scanf("%d",a+i);
memset(tree,0,sizeof(tree));
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
for(i=1;i<=k;i++)
{
scanf("%d",b+i);
updata(b[i],1);
c1[i]=sum(b[i]-1);
c2[i]=sum(b[i]);
}
memset(tree,0,sizeof(tree));
get_next();
memset(tree,0,sizeof(tree));
res=kmp();
printf("%d\n",res);
for(i=0;i<res;i++) printf("%d\n",que[i]);
return 0;
}