【poj 3450 Corporate Identity 】 KMP(暴力)
2016-01-19 20:30
316 查看
C - Corporate Identity
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
题解:求n个串的最长连续公共子串。
思路:1.后缀数组。。。可惜目前不会;
2. 枚举长度,各个KMP一下;
*优化
1.最长连续公共子串可由长度最小串枚举;
2.长度倒枚举,符合break;
3.*可以倒长度+二分枚举;
1.KMP模板:
2.题解:
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
题解:求n个串的最长连续公共子串。
思路:1.后缀数组。。。可惜目前不会;
2. 枚举长度,各个KMP一下;
*优化
1.最长连续公共子串可由长度最小串枚举;
2.长度倒枚举,符合break;
3.*可以倒长度+二分枚举;
1.KMP模板:
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int n; char s[4005][205]; int nex[205]; char c[205]; void get_nex(char s[]) { int j=-1; nex[0]=-1; int len=strlen(s); for(int i=0;i<len;i++) { while(j!=-1&&s[j+1]!=s[i]) j=nex[j]; if(s[i]==s[j+1]&&i!=0) j++; nex[i]=j; } } bool KMP(char s[],char t[]) { int j=-1; int len=strlen(s); int ok=strlen(t); for(int i=0;i<len;i++) { while(j!=-1&&t[j+1]!=s[i]) j=nex[j]; if(s[i]==t[j+1]) //!!! j++; if(j==ok-1) return true; } return false; }
2.题解:
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int n; char s[4005][205]; int nex[205]; char c[205]; void get_nex(char s[]) { int j=-1; nex[0]=-1; int len=strlen(s); for(int i=0;i<len;i++) { while(j!=-1&&s[j+1]!=s[i]) j=nex[j]; if(s[i]==s[j+1]&&i!=0) j++; nex[i]=j; } } bool KMP(char s[],char t[]) { int j=-1; int len=strlen(s); int ok=strlen(t); for(int i=0;i<len;i++) { while(j!=-1&&t[j+1]!=s[i]) j=nex[j]; if(s[i]==t[j+1]) //!!! j++; if(j==ok-1) return true; } return false; }
int main()
{
while(scanf("%d",&n))
{
if(n==0) return 0;
int now=0;
int tmp=205;
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]);
if(tmp>strlen(s[i]))
{
tmp=strlen(s[i]);
now=i;
}
}
char ans[205]="";
int flag=0;
for(int len=tmp;len>=1;len--)
{
for(int i=0;i<=tmp-len;i++)
{
strncpy(c,s[now]+i,len);
c[len]='\0';
get_nex(c);
int j=1;
for(j=1;j<=n;j++)
{
if(j==now) continue;
if(!KMP(s[j],c))
{
j--;
break;
}
}
if(j==n+1)
{
flag=1;
if(strcmp(c,ans)<0||!strlen(ans))
strncpy(ans,c,strlen(c));
}
}
if(flag) break;
}
if(flag==0)
printf("IDENTITY LOST\n");
else
printf("%s\n",ans);
}
}
相关文章推荐
- codeblocks使用openmp
- 《将博客搬至CSDN》
- IOS开发--常用工具类收集整理(Objective-C)(持续更新)
- 钱小写转大写 VB
- Dalvik切换到ART时内存地址对齐问题的分析(1)
- HDU1272 小希的迷宫(并查集+数据坑)
- BOOST线程详解
- SlidingMenu侧滑菜单 两个jar包的导入
- Java IO流(1)
- leetcode之majority number
- 题目1214:丑数
- 264 Ugly Number II
- 简单的变长数组
- 为什么要使用SLF4J而不是Log4J
- 函数返回数组指针
- python实现一个朴素贝叶斯分类器
- 13.使用zabbix icmpping key监控nas
- Hyper-v虚拟机无法初始化
- Androidx学习笔记(10)-- 文件读写操作简介
- 初步了解UBOOT (2)