枚举长度最短的字符串的所有子串,再与其他串匹配。
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=1e5+;
const double eps=1e-;
const double pi=acos(-);
const int inf = 0x3f3f3f3f;
#define ll long long
#define clc(a,b) memset(a,b,sizeof(a)) int next[];
char str[][]; void getnext(char *t)
{
int i=,j=-;
int len=strlen(t);
next[]=-;
while(i<len)
{
if(t[i]==t[j]||j==-)
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
} int kmp(char *s,char *t)
{
int lens=strlen(s);
int lena=strlen(t);
int i=,j=;
while(i<lens&&j<lena)
{
if(s[i]==t[j]||j==-)
{
i++;
j++;
}
else
j=next[j];
}
if(j<lena)
return -;
return i-lena;
} int main()
{
int n,len;
while(cin>>n&&n)
{
char tmp[];
int minn=inf;
for(int i=;i<n;i++)
{
scanf("%s",str[i]);
len=strlen(str[i]);
if(minn>len)
{
minn=len;
strcpy(tmp,str[i]);
}
}
len=strlen(tmp);
char p[];
char f[]={};
int ans=;
for(int i=;i<=len;i++)
{
int cnt;
for(int j=;j+i<=len;j++)
{
cnt=;
strncpy(p,tmp+j,i);
p[i]='\0';
getnext(p);
for(int k=;k<n;k++)
{
if(kmp(str[k],p)!=-)
{
cnt++;
}
else
break;
}
if(cnt==n)
{
ans++;
if(strlen(f)<strlen(p))
strcpy(f,p);
else if(strcmp(f,p)>)
strcpy(f,p);
}
}
}
if(ans==)
printf("IDENTITY LOST\n");
else
printf("%s\n",f);
}
}

POJ 3450 Corporate Identity kmp+最长公共子串的更多相关文章

  1. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  2. POJ 3450 Corporate Identity(KMP)

    [题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...

  3. POJ 2774 Long Long Message [ 最长公共子串 后缀数组]

    题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total ...

  4. POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)

    http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...

  5. poj 3450 Corporate Identity

    题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...

  6. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  7. POJ 3450 Corporate Identity (KMP+暴搞)

    题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...

  8. POJ 3450 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...

  9. POJ3450最长公共子串【kmp】

    题目链接:http://poj.org/problem?id=3450 题目大意:给定n个长度不超过200的字符串,n < 4000.求这些字符串的最长公共子串,若没有,则输出 “IDENTIT ...

随机推荐

  1. Mysql ID重新排列

    我们经常会遇到,在删除数据库某条记录时,原来的ID排序会有间隔,比如删除了ID为8的数据,这个表的ID排序就会从7直接到9, 那我们如何解决这个ID重新排列的问题呢? 只需一下三步: 1.删除这个表的 ...

  2. 宅男福利--利用Python简单爬图

    Ver beta..代码粗陋. 使用说明以Windows为例, Python版本为2.7.6 确认你电脑已经安装了Python, Windows默认安装路径为C:\Python27.如果没有安装,先下 ...

  3. 基于Emgu CV 的手势识别实现PPT的控制放映

    Emgu CV 简介         众所周知,Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版的OpenCV.开发者可以很方便的通过C#,VB等语言调用OpenCV函数 ...

  4. 在asp.net中如何自己编写highcharts图表导出到自己的服务器上来

    1.准备工作:网上下载highcharts导出的关键dll.      1).Svg.dll:因为highcharts的格式其实就是一个xml,采用svg的方式画图:      2).itextsha ...

  5. AOT

    预 (AOT) 编译器 https://angular.cn/docs/ts/latest/cookbook/aot-compiler.html To run your app in AoT mode ...

  6. unity3d 下操作excel 与打印

    原地址:http://www.colabug.com/thread-1146390-1-1.html 因项目需要,需在unity下实现写入excel.打印功能.因之前在winform里操作过excel ...

  7. POJ2225+BFS

    简单的BFS   1a /* 从起点到终点 */ #include<stdio.h> #include<string.h> #include<stdlib.h> # ...

  8. PHP file_exists() 函数

    定义和用法 file_exists() 函数检查文件或目录是否存在. 如果指定的文件或目录存在则返回 true,否则返回 false. 语法 file_exists(path) 参数 描述 path ...

  9. Linux下SSH免密码登录

    转自:http://haitao.iteye.com/blog/1744272 ssh配置 主机A:10.0.5.199 主机B:10.0.5.198 需要配置主机A无密码登录主机A,主机B 先确保所 ...

  10. 转发:maven打包时始终出现以下提示:-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)

    maven打包时始终出现以下提示: 1.-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)List<User> userList= new Array ...