HDU 2328 Corporate Identity kmp暴力

一、 内容

 Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

Your task is to find such a sequence. 

Input

The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.

After the last trademark, the next task begins. The last task is followed by a line containing zero.

Output

For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.

Sample Input

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

Sample Output

abb
IDENTITY LOST

二、思路

三、代码

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 205, M = 4005;
string s[M], p, ans;
int k;
int main() {
	while (scanf("%d", &k), k) {
		for (int i = 1; i <= k; i++) cin >> s[i]; 
		//枚举所有字串
		int n = s[1].size(); ans = "";
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				p = s[1].substr(i, j + 1);
				int h;
				for (h = 1; h <= k; h++) {
					if (s[h].find(p) == string::npos) break;
				} 
				if (h == k + 1) {
					if (p.size() > ans.size()) ans = p;
					else if (p.size() == ans.size() && ans > p) ans = p;
				}
			}
		} 
		if (!ans.size()) printf("IDENTITY LOST\n");
		else cout << ans << endl;
	}
	return 0;
}
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 205, M = 4005;
char s[M][N], p[N], ans[N];
int  m, mx, t, k, ne[N];
void getNext() {
	ne[1] = 0;
	for (int i = 2, j = 0; i <= m; i++) {
		while (j && p[i] != p[j + 1]) j = ne[j];
		if (p[i] == p[j + 1]) j++;
		ne[i] = j;
	}
}
bool kmp(int k) {
	int n = strlen(s[k] + 1);
	getNext();
	for (int i = 1, j = 0; i <= n; i++) {
		while (j && s[k][i] != p[j + 1]) j = ne[j];
		if (s[k][i] == p[j + 1]) j++;
		if (j == m) return true;//匹配成功 
	}
	return false;
}
bool ok() {
	for (int i = 1; i <= k; i++) {
		if (!kmp(i)) return false;
	}
	return true;
} 
int main() {
	while (scanf("%d", &k), k) {
		for (int i = 1; i <= k; i++) scanf("%s", s[i] + 1); 
		//枚举所有字串
		int n = strlen(s[1] + 1); mx = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = i; j <= n; j++) {
				for (int h = i; h <= j; h++) p[h - i + 1] = s[1][h]; 
				m = j - i + 1; p[m + 1] = '\0';
				if (ok()) {
					if (m > mx) { mx = m; memcpy(ans + 1, p + 1, sizeof(p));}
					else if(m == mx && strcmp(ans + 1, p + 1) > 0) memcpy(ans + 1, p + 1, sizeof(p));
				}  
			}
		} 
		if (!mx) printf("IDENTITY LOST\n");
		else printf("%s\n", ans + 1);
	}
	return 0;
}
发布了499 篇原创文章 · 获赞 516 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104702586
今日推荐