HDU 4749 Parade Show(KMP)

Parade Show

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 921    Accepted Submission(s): 386


Problem Description

  2013 is the 60 anniversary of Nanjing University of Science and Technology, and today happens to be the anniversary date. On this happy festival, school authority hopes that the new students to be trained for the parade show. You should plan a better solution to arrange the students by choosing some queues from them preparing the parade show. (one student only in one queue or not be chosen)
  Every student has its own number, from 1 to n. (1<=n<=10^5), and they are standing from 1 to n in the increasing order the same with their number order. According to requirement of school authority, every queue is consisted of exactly m students. Because students who stand adjacent in training are assigned consecutive number, for better arrangement, you will choose in students with in consecutive numbers. When you choose these m students, you will rearrange their numbers from 1 to m, in the same order with their initial one. 
  If we divide our students’ heights into k (1<=k<=25) level, experience says that there will exist an best viewing module, represented by an array a[]. a[i] (1<=i<=m)stands for the student’s height with number i. In fact, inside a queue, for every number pair i, j (1<=i,j<=m), if the relative bigger or smaller or equal to relationship between the height of student number i and the height of student number j is the same with that between a[i] and a[j], then the queue is well designed. Given n students’ height array x[] (1<=x[i]<=k), and the best viewing module array a[], how many well designed queues can we make at most?
 

Input
Multiple cases, end with EOF.
First line, 3 integers, n (1<=n<=10^5) m (1<=m<=n) k(1<=k<=25),
Second line, n students’ height array x[] (1<=x[i]<=k,1<=i<=n);
Third line, m integers, best viewing module array a[] (1<=a[i]<=k,1<=i<=m);
 

Output
One integer, the maximal amount of well designed queues.
 

Sample Input

10 5 10 2 4 2 4 2 4 2 4 2 4 1 2 1 2 1
 

Sample Output

1

KMP的变形题,关键在于处理相对高度这个问题。

一个个匹配过去,如果前缀匹配,并且加上当前字母后,前面比这个字母小的,相等的,大于的数字个数都相等,就是匹配,剩下的就是KMP了。

代码:

#include 
#include 

const int N = 100005;
const int M = 26;
int n, m, k, seq[N], seq1[N], next[N], i, t, sum1[M][N], sum2[M][N];

bool judge(int i, int j, int sum1[][N], int sum2[][N], int *seq, int *seq1) {
	int eq1 = 0, lt1 = 0, eq2 = 0, lt2 = 0;
	for (int x = 1; x <= k; x++) {
		if (x < seq[i]) {
			lt1 += sum1[x][i] - sum1[x][i - j];
		}
		else if (x == seq[i]) {
			eq1 += sum1[x][i] - sum1[x][i - j];
		}
		if (x < seq1[j]) {
			lt2 += sum2[x][j];
		}
		else if (x == seq1[j]) {
			eq2 += sum2[x][j];
		}
	}
	return (lt1 == lt2 && eq1 == eq2);
}

void get_next(int *seq, int m) {
	memset(next, 0, sizeof(next));
	int j = 0;
	for (int i = 2; i <= m; i++) {
		while (j > 0 && !judge(i, j + 1, sum2, sum2, seq1, seq1))
			j = next[j];
		if (judge(i, j + 1, sum2, sum2, seq1, seq1)) j++;
		next[i] = j;
	}
}

int kmp(int *seq, int *seq1, int n, int m) {
	int j = 0, ans = 0;
	for (int i = 1; i <= n; i++) {
		while (j > 0 && !judge(i, j + 1, sum1, sum2, seq, seq1)) j = next[j];
		if (judge(i, j + 1, sum1, sum2, seq, seq1)) j++;
		if (j == m) {
			ans++;
			j = 0;
		}
	}
	return ans;
}

int main() {
	while (~scanf("%d%d%d", &n, &m, &k)) {
		memset(sum1, 0, sizeof(sum1));
		memset(sum2, 0, sizeof(sum2));
		for (i = 1; i <= n; i++) {
			scanf("%d", &seq[i]);
			for (int j = 1; j <= k; j++)
				sum1[j][i] = sum1[j][i - 1];
			sum1[seq[i]][i]++;
		}
		for (i = 1; i <= m; i++) {
			scanf("%d", &seq1[i]);
			for (int j = 1; j <= k; j++)
				sum2[j][i] = sum2[j][i - 1];
			sum2[seq1[i]][i]++;
		}
		get_next(seq1, m);
		printf("%d\n", kmp(seq, seq1, n, m));
	}
	return 0;
}


你可能感兴趣的:(数据结构-KMP)