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 & ...
 
随机推荐
- Guice总结
			
Guice总结 Jar包:guice-4.1.0.jar 辅包: guava-15.0.jar aopalliance-.jar javaee-api-6.0-RC2.jar Guice的IoC 两种 ...
 - POJ3126Prime Path(BFS)
			
#include"cstdio" #include"queue" #include"cstring" using namespace std ...
 - bzoj4545
			
lct+SAM bzoj4516+bzoj2555 这道题唯一的用处就是教会了我真正的广义SAM dfs时保留当前节点在后缀自动机中的位置,每个点接着父亲建 lct动态维护right集合大小,用lct ...
 - hibernate 数据关联一对一
			
第一种一对一 person和card,card的id即作为主键,又作为外键 // 各村对方的一个对象 public class Person { private Integer id; privat ...
 - Queue——C#浅谈
			
1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...
 - 问题:sqlserver if;结果: SqlServer if else和case
			
SqlServer if else和case 分类: SQLSERVER 2013-03-01 16:51 11328人阅读 评论(0) 收藏 举报 行转列 目录(?)[+] if else 要提示的 ...
 - 六种获取配置properties文件的方法
			
总结一下六种获取配置properties文件的方法,代码如下: package com.xujingyang.test ; import java.io.BufferedInputStream ; i ...
 - nodejs 操作文件系统读取写入文件
			
我们通过fs这个模块来对文件系统进行操作,对于文件系统操作一般都有同步.异步方法,两者区别,同步等有返回结果时候,在继续执行后面的代码,异步是不等返回结果,直接执行后面的代码,待有返回结果时候,通过回 ...
 - 朴素贝叶斯算法分析及java 实现
			
1. 先引入一个简单的例子 出处:http://www.ruanyifeng.com/blog/2013/12/naive_bayes_classifier.html 一.病人分类的例子 让我从一个例 ...
 - 23. Bypass ngx_lua_waf SQL注入防御(多姿势)
			
0x00 前言 ngx_lua_waf是一款基于ngx_lua的web应用防火墙,使用简单,高性能.轻量级.默认防御规则在wafconf目录中,摘录几条核心的SQL注入防御规则: select.+(f ...