码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 2328 Corporate Identity

时间:2014-09-16 20:41:21      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   os   ar   for   div   sp   log   

Description

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int Maxn = 4010;
const int N = 210;

int n, next[Maxn];
char words[Maxn][N];

int cmp(const void *str1, const void *str2) {
	return strlen((char *)str1) - strlen((char *)str2);
}

void getNext(char *str) {
	int m = strlen(str);
	next[0] = next[1] = 0;
	for (int i = 1; i < m; i++) {
		int j = next[i];
		while (j && str[i] != str[j])
			j = next[j];
		next[i+1] = str[i] == str[j] ? j+1 : 0;
	}
}

int kmp(char *text, char *str) {
	getNext(str);
	int n = strlen(text);
	int m = strlen(str);
	int j = 0;
	for (int i = 0; i < n; i++) {
		while (j && text[i] != str[j])
			j = next[j];
		if (str[j] == text[i])
			j++;
		if (j == m)
			return 1;
	}
	return 0;
}

int main() {
	while (scanf("%d", &n) != EOF && n) {
		for (int i = 0; i < n; i++)
			scanf("%s", words[i]);
		qsort(words, n, sizeof(words[0]), cmp);
		char ans[Maxn], tmp[Maxn];
		int len = strlen(words[0]);
		int Max = 0;
		for (int i = 0; i < len; i++) {
			memset(tmp, '\0', sizeof(tmp));
			int pos = 0;
			for (int j = i; j < len; j++) {
				tmp[pos++] = words[0][j];
				getNext(tmp);

				int flag = 1;
				for (int k = 1; k < n; k++)
					if (kmp(words[k], tmp) == 0) {
						flag = 0;
						break;
					}
				if (flag && Max < pos) {
					Max = pos;
					memcpy(ans, tmp, sizeof(tmp));
				}
				else if (flag && Max == pos && strcmp(ans, tmp) > 0)
					memcpy(ans, tmp, sizeof(tmp));
			}
		}

		if (Max)
			printf("%s\n", ans);
		else printf("IDENTITY LOST\n");
	}
	return 0;
}


HDU - 2328 Corporate Identity

标签:des   style   io   os   ar   for   div   sp   log   

原文地址:http://blog.csdn.net/u011345136/article/details/39321287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!