福州大學第十屆校賽 & fzu 2128最長子串

思路: 對於每個子串,求出 母串中 所有該子串 的 開始和結束位置,保存在 mark數組中,求完所有子串後,對mark數組按 結束位置排序,然後 用後一個的結束位置 減去 前一個的 開始 位置 再 減去 1,記錄最大值
比如 aaaqwer  1  aaa 那麼 最長爲 aaqwer

用strstr判斷子串是否存在於母串中。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1000005;

struct MARK {
	int begin, end;
	bool operator <(const MARK& cmp) const {
		return end < cmp.end;
	}
} mark[maxn];

char s[maxn], t[1005][105];
int n, cnt, next[105];

void work(const char str[], const char sub[]) {
	//memset(next, 0, sizeof(next));
	//get_next(sub, next);
	int exp = 0, from, len = strlen(sub);
	while (strstr(str + exp, sub) != NULL) {
		from = strstr(str + exp, sub) - str;
		mark[cnt].begin = from;
		mark[cnt].end = from + len - 1;
		cnt++;
		exp = from + len - 1;
	}
}

int main() {
	while (scanf("%s", s) == 1) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%s", t[i]);
		cnt = 0;
		for (int i = 0; i < n; i++) {
			work(s, t[i]);
		}
		mark[cnt].begin = mark[cnt].end = strlen(s);
		cnt++;
		sort(mark, mark + cnt);
		int ans = -1;
		for (int i = 0; i < cnt - 1; i++) {
			int len = mark[i + 1].end - mark[i].begin - 1;
			if (len > ans)
				ans = len;
		}
		printf("%d\n", ans == -1 ? strlen(s) : ans);
	}
}



用KMP判斷子串是否存在於母串中。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1000005;

struct MARK {
	int begin, end;
	bool operator <(const MARK& cmp) const {
		return end < cmp.end;
	}
} mark[maxn];

char s[maxn], t[1005][105];
int n, cnt, next[105];

void get_next(const char *sub, int *next) {
	int len = strlen(sub);
	int i, k;
	next[0] = k = -1;
	for (i = 0; i < len;) {
		if (k == -1 || sub[i] == sub[k]) {
			k++;
			i++;
			if (sub[k] != sub[i])
				next[i] = k;
			else
				next[i] = next[k];
		} else
			k = next[k];
	}
}

int KMP(const char *str, const char *sub, const int *next) {
	int i, j;
	int len1 = strlen(str), len2 = strlen(sub);
	for (i = 0, j = 0; i < len1 && j < len2;) {
		if (j == -1 || str[i] == sub[j]) {
			i++;
			j++;
		} else
			j = next[j];
	}
	if (j == len2)
		return i - len2;
	return -1;
}

void work(const char str[], const char sub[]) {
	//memset(next, 0, sizeof(next));
	get_next(sub, next);
	int exp = 0, from = 0, len = strlen(sub);
	while ((from = KMP(str + exp, sub, next)) != -1) {
		mark[cnt].begin = from + exp;
		mark[cnt].end = from + exp + len - 1;
		cnt++;
		exp += from + len - (len == 1 ? 0 : 1);
	}
}

int main() {
	while (scanf("%s", s) == 1) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%s", t[i]);
		cnt = 0;
		for (int i = 0; i < n; i++) {
			work(s, t[i]);
		}
//		for (int i = 0; i < cnt; i++)
//			printf("%d %d\n", mark[i].begin, mark[i].end);
		mark[cnt].begin = mark[cnt].end = strlen(s);
		cnt++;
		sort(mark, mark + cnt);
		int ans = -1;
		for (int i = 0; i < cnt - 1; i++) {
			int len = mark[i + 1].end - mark[i].begin - 1;
			if (len > ans)
				ans = len;
		}
		printf("%d\n", ans == -1 ? strlen(s) : ans);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章