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.
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.
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.
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.
题意:
给定2个串,求主串中有哪些连续子串可以和模式串匹配上,使得偏序结果相同。
2个长度一样的串的偏序相同的条件是,对于每个位置的2个数,他们的前面小于他们的数一样多,等于他们的数也一样多。
所以还是要用KMP匹配,而且是可覆盖的匹配
代码:
#include<iostream>
using namespace std;
int s[100005], t[25005], nums[100005][26], numt[25005][26], nextt[25005], ans[100005];//nums[i][j]是s前i个数中不超过j的数的个数
bool issame(int nums[][26], int numt[][26], int x, int y,int kx,int ky)
{
return numt[y][ky] == nums[x][kx] - nums[x - y][kx] && numt[y][ky - 1] == nums[x][kx - 1] - nums[x - y][kx - 1];
}
void getnext(int m, int *next) //m是串的长度
{
int i = 1, j = 0;
next[0] = next[1] = 0;
while (i < m)
{
if (j == 0 || issame(numt, numt, i, j, t[i], t[j]))next[++i] = ++j;
else j = next[j];
}
}
int main()
{
ios::sync_with_stdio(false);
int n, k, m;
cin >> n >> k >> m;
for (int i = 0; i <= m; i++)nums[0][i] = numt[0][i] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)nums[i][j] = nums[i - 1][j];
cin >> s[i];
for (int j = s[i]; j <= m; j++)nums[i][j]++;
}
for (int i = 1; i <= k; i++)
{
for (int j = 0; j <= m; j++)numt[i][j] = numt[i - 1][j];
cin >> t[i];
for (int j = t[i]; j <= m; j++)numt[i][j]++;
}
getnext(k, nextt);
int ii = 0, j = 0, ansk = 0;
while (ii <= n)
{
if (j == 0)ii++, j++;
else if (issame(nums, numt, ii, j,s[ii],t[j]))
{
ii++, j++;
if (j > k)ans[ansk++] = --ii - --j + 1, j = nextt[j];
}
else j = nextt[j];
}
cout << ansk << endl;
for (int i = 0; i < ansk; i++)cout << ans[i] << endl;
return 0;
}