# poj3167 Cow Patterns

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:

``````  1 4 4 3 2 1
``````

In 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 9

then only the subsequence 2 10 10 7 3 2 matches FJ’s pattern above.

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.

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.

tot1[j+1]==qry(a[i]-1)&&tot2[j+1]==qry(a[i])

``````#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
vector<int> ans;
int a[100010],b[25010],next[25010],sum[30],cnt[30],s,tot1[25010],tot2[25010];
int qry(int x)
{
int ans=0;
for (int i=1;i<=x;i++)
ans+=sum[i];
return ans;
}
int main()
{
int i,j,k,m,n,p,q,x,y,z;
scanf("%d%d%d",&n,&m,&s);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (i=1;i<=m;i++)
scanf("%d",&b[i]);
for (i=1;i<=m;i++)
{
tot1[i]=qry(b[i]-1);
tot2[i]=qry(b[i]);
sum[b[i]]++;
}
memset(sum,0,sizeof(sum));
for (i=2,j=0;i<=m;i++)
{
while (j&&(tot1[j+1]!=qry(b[i]-1)||tot2[j+1]!=qry(b[i])))
{
for (k=i-j;k<i-next[j];k++)
sum[b[k]]--;
j=next[j];
}
if (tot1[j+1]==qry(b[i]-1)&&tot2[j+1]==qry(b[i])) j++;
next[i]=j;
sum[b[i]]++;
}
memset(sum,0,sizeof(sum));
for (i=1,j=0;i<=n;i++)
{
while (j==m||(j&&(tot1[j+1]!=qry(a[i]-1)||tot2[j+1]!=qry(a[i]))))
{
for (k=i-j;k<i-next[j];k++)
sum[a[k]]--;
j=next[j];
}
if (tot1[j+1]==qry(a[i]-1)&&tot2[j+1]==qry(a[i])) j++;
if (j==m) ans.push_back(i-m+1);
sum[a[i]]++;
}
printf("%d\n",ans.size());
for (i=0;i<ans.size();i++)
printf("%d\n",ans[i]);
}``````

• 本文已收录于以下专栏：

## POJ 3167 Cow Patterns

• Fallen_Breath
• 2016年09月26日 13:09
• 332

## poj3167

• u013491262
• 2015年10月01日 21:53
• 396

## 【KMP】【poj3167】【bzoj1729】Cow Patterns

• peter_819
• 2017年03月25日 21:40
• 305

## poj3167 Cow Patterns

• sdfzyhx
• 2016年08月28日 15:29
• 425

## POJ 3167 Cow Patterns (KMP + 树状数组)

• 2013年10月02日 20:41
• 1173

## [kmp] POJ3167 Cow Patterns

Cow Patterns Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3849 Accepted: 13...
• u012848631
• 2015年07月11日 17:17
• 254

## POJ3167 Cow Patterns (KMP)

Description A particular subgroup of K (1
• chen_logos
• 2015年07月09日 19:29
• 300

## HDU 4749 && POJ 3167 KMP

RT 这个题是POJ3167的修改版，基本一致   #include #include #include #include #include #include #include #inc...
• qq574857122
• 2013年09月21日 18:02
• 2670

## POJ3295 Tautology (遞歸+構造)

Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing so...
• chen_logos
• 2015年04月19日 22:32
• 217

## poj3167 Cow Patterns【KMP】

• cdsszjj
• 2018年01月27日 16:13
• 33

举报原因： 您举报文章：poj3167 Cow Patterns 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)