POJ - 3450
题目链接:http://poj.org/problem?id=3450
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8549 | Accepted: 2856 |
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
Source
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=4e3+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][];
int next[maxn];
void cal_next(char s[])
{
int len=strlen(s);
int k=-;
next[]=-;
for(int i=;i<len;i++)
{
while(k>-&&s[k+]!=s[i])
{
k=next[k];
}
if(s[k+]==s[i]) k++;
next[i]=k;
}
}
bool kmp(char x[],char y[])
{
int k=-;
int len1=strlen(x);
int len2=strlen(y);
for(int i=;i<len1;i++)
{
while(k>-&&y[k+]!=x[i])
{
k=next[k];
}
if(x[i]==y[k+]) k++;
if(k==len2-) return true;
}
return false;
}
int main()
{
//ios::sync_with_stdio(false);
int n,flag=;
while(scanf("%d",&n)!=EOF)
{
getchar();
if(n==) break;
char ans[]="",temp[];
scanf("%s",a[]);
getchar();
strcpy(temp,a[]);//这里不用自己加'\0',因为a[0]本身就有'\0'的,直接复制过去了
for(int i=;i<n;i++)
{
scanf("%s",a[i]);
getchar();
if(strlen(a[i])<strlen(temp))
{
strcpy(temp,a[i]);
}
}
int len=strlen(temp);
for(int i=;i<len;i++)//起点
{
for(int j=;i+j<=len;j++)//长度
{
char op[];
strncpy(op,temp+i,j);//不会在结尾给你自动加'\0'
op[j]='\0';//注意这里手动加一个'\0',因为你只是得到了那几个字符,并没有得到'\0'
cal_next(op);
for(int k=;k<n;k++)
{
flag=;
if(!kmp(a[k],op))
{
flag=;
break;
}
}
if(flag==)//有一个找不到就不用遍历以该起点长度更大的子串了,因为肯定也会找不到
break;
else
{
if(strlen(op)>strlen(ans)) strcpy(ans,op);
else if(strlen(op)==strlen(ans)&&strcmp(ans,op)>)
{
strcpy(ans,op);
}
}
}
}
if(strlen(ans)==)
printf("IDENTITY LOST\n");
else
printf("%s\n",ans);
}
return ;
}
看代码2:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=5e3+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][];
int next[maxn];
void cal_next(char op[])
{
int k=-;
next[]=-;
int len=strlen(op);
for(int i=;i<len;i++)
{
while(k>-&&op[k+]!=op[i])
{
k=next[k];
}
if(op[k+]==op[i]) k++;
next[i]=k;
}
return ;
}
bool kmp(char x[],char y[])
{
int k=-;
int len1=strlen(x);
int len2=strlen(y);
for(int i=;i<len2;i++)
{
while(k>-&&x[k+]!=y[i])
{
k=next[k];
}
if(x[k+]==y[i]) k++;
if(k==len1-) return true;
}
return false;
}
int main()
{
int n,flag;
char b[],temp[];
while(scanf("%d",&n)!=EOF)
{
int minn=;
if(n==) break;
getchar();
char ans[]="";
for(int i=;i<n;i++)
{
scanf("%s",a[i]);
int len=strlen(a[i]);
if(len<minn)
{
minn=len;
strcpy(b,a[i]);
}
getchar();
}
for(int i=;i<=minn;i++)//长度
{
for(int j=;j+i<=minn;j++)//起点
{
flag=;
strncpy(temp,b+j,i);
temp[i]='\0'; cal_next(temp);
for(int k=;k<n;k++)
{
if(!kmp(temp,a[k]))
{
flag=;
break;
}
}
if(flag==)
{
if(strlen(temp)>strlen(ans)) strcpy(ans,temp);
else if(strlen(temp)==strlen(ans)&&strcmp(ans,temp)>) strcpy(ans,temp);
}
}
}
if(strlen(ans)==) printf("IDENTITY LOST\n");
else
{
printf("%s\n",ans);
}
}
}
POJ - 3450的更多相关文章
- 字符串截取模板 && POJ 3450、3080 ( 暴力枚举子串 && KMP匹配 )
//截取字符串 ch 的 st~en 这一段子串返回子串的首地址 //注意用完需要根据需要最后free()掉 char* substring(char* ch,int st,int en) { ; c ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- HDU 2328 POJ 3450 KMP
题目链接: HDU http://acm.hdu.edu.cn/showproblem.php?pid=2328 POJhttp://poj.org/problem?id=3450 #include ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ 3450 Corporate Identity (KMP+暴搞)
题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...
随机推荐
- BZOJ1216:[HNOI2003]操作系统
我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...
- bzoj 3730 震波 —— 动态点分治+树状数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3730 建点分树,每个点记两个树状数组,存它作为重心管辖的范围内,所有点到它的距离情况和到它在 ...
- Centos6.5上的iptables
1.Centos6.5默认开启了iptables 当Centos6.5上安装了MySQL后,在远程连接它,如果出现10060的错误,说明iptables在起作用. 关闭iptables即可,sudo ...
- asp.net分页asp.net无刷新分页高效率分页
项目中经常会用到分页的功能类似的项目做过无数个了,今个把自己常用的分页代码分享一下. 首先说说服务端处理的代码: 下面代码中重点是分页的sql语句的写法,其中的参数@n是当前的页码,总的来说本服务端主 ...
- 使用svnadmin对VisualSVN进行项目迁移
使用svnadmin对VisualSVN进行项目迁移 导出1> 启动命令行cmd2> 运行%VISUALSVN_SERVER%\bin\svnadmin dump PATH-TO-REPO ...
- python 基础 列表 字符串转换
1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'worl ...
- 推荐!Html5精品效果源码分享
一直在看别人的汇总,看到了一些不错的关于 HTML5内容的源码,我也汇总下分享出来,好东西需要共享!希望可以帮到需要的朋友. 1.劲爆分享:HTML5动感的火焰燃烧动画特效 这又是一款基于HTML5的 ...
- %.*s, printf
%.*s_百度搜索 c语言%.*s是什么_百度知道 *用来指定宽度,对应一个整数 .(点)与后面的数合起来 是指定必须输出这个宽度,如果所输出的字符串长度大于这个数,则按此宽度输出,如果小于,则输出实 ...
- /* font-awesome-4.7.0的应用*/
<!DOCTYPE html> /* font-awesome-4.7.0的应用*/ <html lang="en"> <head> <m ...
- 一些奇怪的Javascript用法
阅读AngularJS时,看到一些奇怪的Javascript用法.1.(function(){ a.work=function(){} })(a) 声明一个匿名函数并执行 2. ...