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 & ...
随机推荐
- 【转】 Pro Android学习笔记(二八):用户界面和控制(16):GridLayout
网格布局:GridLayout 我个人觉得GridLayout的设计还不很完善,每个网格的大小,由填充的cell决定,即默认是wrap很容易整个GridLayout超出屏幕.下面是一个例子: < ...
- js中的setInterval
跟几个例子吧 计时器的例子: /** * Created by Administrator on 2016/8/5. */ (function () { function show() { var t ...
- 关于redis,学会这8点就够了(转)
1.redis是什么 redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串.哈希.列表.队 ...
- javaScript之跨浏览器的事件对象
跨浏览器的兼容代码 var eventHandler = { addHandler: function(element, type, handler){}, removeHandler: functi ...
- sharepoint Foundation 2013安装过程
安装完必备软件后,便可安装sharepoint Foundation 2013
- Centos下添加/删除用户
useradd具体参数 [root@yhwang ~] useradd -h Usage: useradd [options] LOGIN useradd -D useradd -D [options ...
- 《精通Spring4.X企业应用开发实战》读后感第五章(FactoryBean)
- 移除KVO的风险
为之前项目添加一个功能用到了一个开源库XMTextView,然后运行报错提示: 显示没有注册一个叫font的观察者,所以闪退.但是我的UITextView没有添加观察者呀,怎么会删除呢? 原来是由分类 ...
- C#文件监控工具(对追加内容的监控并输出)
C#文件监控(对追加内容的监控并输出),适合监控某个目录下的日志文件(log),开发初衷是linux上部署在jexus部署网站后想实时输出jexus的log和自己站点的log文件(已经测试通过在mon ...
- C#监听窗体新建/鼠标移入移出
在新建window窗体时会激活方法,并循环所有窗体,鼠标移动在重写方法的页面中也会激活 winform直接在继承了From窗体cs中 protected override void WndProc(r ...