http://acm.hdu.edu.cn/showproblem.php?pid=2328

Corporate Identity

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 698    Accepted Submission(s): 281

Problem 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
 
其实就是一个暴力解决的问题,以我现在的程度只能暴力,可是我也想写个更好的方法呀,下次一定要学
 
可坑了,我没有给我的存子串的串赋初值,导致我在用strcmp的时候老是出错,弄的我稀里糊涂的写了个Judge函数用来比较串,可道理是一样的呀,于是又wa了,还好在队友的帮助下找到了,初值这个问题不知道害了我多少次,可我还往里面跳,以后要更加注意了,有的错找不出来了,可以往初值上想想,另外以后要注意赋初值

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define M 40005
#define N 210 char s[M][N];
int Next[N]; void FindNext(char b[])
{
int i=, j=-, blen=strlen(b);
Next[] = -; while(i<blen)
{
if(j==- || b[i]==b[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int KMP(char a[], char b[])
{
int i=, j=;
int alen=strlen(a), blen=strlen(b); FindNext(b); while(i<alen)
{
while(j==- || (a[i]==b[j] && i<alen && j<blen))
i++, j++;
if(j==blen)
return ;
j = Next[j];
}
return ;
} int main()
{
int n;
while(scanf("%d", &n), n)
{
int i, j, k, MinLen=, len;
char ss[N]; memset(s, , sizeof(s)); for(i=; i<n; i++)
{
scanf("%s", s[i]);
len = strlen(s[i]); if(len<MinLen)
{
MinLen = len;
memset(ss, , sizeof(ss));
strcpy(ss, s[i]);
}
} char b[N]="{";
int index=; for(i=MinLen; i>; i--)
{
for(j=; j<=MinLen-i; j++)
{
char a[N]; memset(a, , sizeof(a)); strncpy(a, ss+j, i); for(k=; k<n; k++)
{
if(KMP(s[k], a)==)
break;
} if(k==n && strcmp(a, b)<)
{
index = i;
strcpy(b, a);
} if(index && j==MinLen-i)
i=-, j=;
}
} if(index)
printf("%s\n", b);
else
printf("IDENTITY LOST\n"); }
return ;
}

(KMP 暴力)Corporate Identity -- hdu -- 2328的更多相关文章

  1. Corporate Identity - HDU 2328(多串求共同子串)

    题目大意:给你N(2-4000)个字符串,求出来他们的共同子串   分析:因为上次就说了再出现这种题就不用那种暴力的做法了,于是看了一些别的知识,也就是后缀树,把一个字符串的所有的后缀全部都加入字典树 ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. hdu 2328 Corporate Identity(kmp)

    Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...

  4. hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

    Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. POJ 题目3450 Corporate Identity(KMP 暴力)

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 201 ...

  6. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  7. hdu2328 Corporate Identity 扩展KMP

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  8. hdu2328 Corporate Identity

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...

  9. N - Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

随机推荐

  1. Can not find the tag library descriptor for "http://www.springframework.org/tags"

    1.Download the Spring dependency jar2.Place it to the lib folder path is /WEB-INF/lib/spring.jar 3.T ...

  2. hadoop无法启动常见原因

    1.Could not chdir to home directory /home/USER: Permission denied 启动datanode时会报这个错误,尝试利用ssh登录datanod ...

  3. js中常见的创建对象的方法

    前两天好好的把高程对象那一块又读了下,顺便写点笔记.补一句:代码都测试过了,应该没有问题的.可以直接拿到控制台跑! 1.工厂模式 function person(name, age, job) { v ...

  4. centos7 升级python2.7 到python3.6(Centos7 安装Anaconda)

    Anaconda 下载 https://www.anaconda.com/download/#linux 下载文件 Anaconda3-5.2.0-Linux-x86_64.sh bash Anaco ...

  5. electron 大体结构

    1.Electron支持的平台: OS XWindowsLinux 2.一个标准的electron app包含的结构: Windows 或是 Linux中:electron/resources/app ...

  6. ValueError: update only works with $ operators

    问题:在执行pymongo的update语句时,提示了ValueError: update only works with $ operators 脚本:db.user.update_one({&qu ...

  7. IE6 PNG不透明问题 (只解决img标签的图片)

    <script type='text/javascript' src="/script/ie6.pngfix.js"></script> 会让一些posit ...

  8. Django之ORM使用以及模板语言

    一.ORM版增删改查 1.ORM的语句 1.类名.objects.all()          --> 返回一个列表 2.类名.objects.filter()       --> 返回一 ...

  9. IO模型与select,poll,epoll

    五种:阻塞,非阻塞,IO复印,信号驱动,异步. select,poll,epoll select: 典型用32个32位的整数表示1024个描述符,并发的局限. poll:功能同上,但数据结构不一样(链 ...

  10. python之字典【dict】

    #Auther Bob#--*--conding:utf-8 --*-- #创建一个字典dictdic1 = {'k1':'v1','k2':'v2'}dic2 = dict(k1='v1',k2=' ...