统计单词个数

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int len = 0;
char s[300];
int p, k;
char wl;
void dr() {
	while (p--) {
		for (int i = 0; i < 20; i++) {
			scanf(" %c", &wl);
			s[len++] = wl;
		}
	}
}
char dt[10][300] = {0};
int gs;
int kmp[10][300] = {0};
int ln[10];
bool visit[300];
int curMatch[10];
int sum;
int ct(int l, int r) {
	sum = 0;
	memset(visit, false, sizeof(visit));
	memset(curMatch, 0, sizeof(curMatch));
	for (int i = l; i <= r; i++) {
		for (int j = 0; j < gs; j++) {
			while (curMatch[j] && s[i] != dt[j][curMatch[j]])curMatch[j] = kmp[j][curMatch[j]];
			if (s[i] == dt[j][curMatch[j]])curMatch[j]++;
			if (curMatch[j] == ln[j] && !visit[i - ln[j] + 1]) {
				visit[i - ln[j] + 1] = true;
				sum++;
			}
		}
	}
	return sum;
}
int main() {
	scanf("%d%d", &p, &k);
	dr();
	scanf("%d", &gs);
	for (int i = 0; i < gs; i++)scanf("%s", dt[i]);
	int x;
	for (int i = 0; i < gs; i++) { //
		kmp[i][0] = kmp[i][1] = 0;
		ln[i] = strlen(dt[i]);
		x = 0;
		for (int j = 1; j < ln[i]; j++) {
			while (x && dt[i][x] != dt[i][j])x = kmp[i][x];
			kmp[i][j + 1] = dt[i][x] == dt[i][j] ? ++x : 0;
		}
	}
	int f[300][300] = {0};
	for (int i = 0; i < len; i++) {
		for (int j = 1; j <= k; j++) {
			if (j == 1) {
				f[i][j] = ct(0, i); //
			}            else {
				for (int k = 0; k < i; k++) {
					if (f[k][j - 1])
						f[i][j] = max(f[i][j], f[k][j - 1] + ct(k + 1, i)); //
				}
			}
		}
	}
	printf("%d", f[len - 1][k]); //
	return 0;
}

上一篇:E-MapReduce Kafka Kerberos集群授权


下一篇:idea的debug调试方法!!!