枚举长度最短的字符串的所有子串,再与其他串匹配。
 #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. Yii Query Builder insert()、update()、delete()使用

    Yii自带的query builder还是很好用的,省去了拼sql的过程,今天在写一个语句的时候遇到这样一个问题 $connection = Yii::app()->db; $command = ...

  2. hdu 2594 Simpsons’ Hidden Talents KMP应用

    Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...

  3. log4j配置文件写法

    ### direct log messages to stdout ###log4j.rootLogger=DEBUG,stdoutlog4j.appender.stdout=org.apache.l ...

  4. UIViewCotroller 的生命周期函数

    Viewcontroller 的所有生命周期函数 重写时 一定要先写 父类 方法 就是(super  +生命周期函数) LoadView ViewDidLoad ViewDidUnload: 在iOS ...

  5. CODEVS 1004四子连棋

    [题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...

  6. CODEVS 2102 石子归并 2

    [题目描述 Descriptin] 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分.试设计出1个算法, ...

  7. Linux开启服务器问题(李蕾问题)

    每次启动192.168.1.223服务器时,都要执行这个命令! #:service iptables stop

  8. 映像备份与恢复管理工具Easy Image X使用说明

    Easy Image X(简称EIX)是一个支持Ghost映像(.gho)和ImageX映像(.wim)的映像管理工具,具有友好的图形界面,仅需几步简单操作即可完成映像备份与恢复工作.维护时使用最多的 ...

  9. [转]关于MYSQL Innodb 锁行还是锁表

    关于mysql的锁行还是锁表,这个问题,今天算是有了一点头绪,mysql 中 innodb是锁行的,但是项目中居然出现了死锁,锁表的情况.为什么呢?先看一下这篇文章. 目时由于业务逻辑的需要,必须对数 ...

  10. WPS目录制作方法

    学校安排我进行电子技术校本教材的后期制作,汇总完全部文字后,需要编辑一个全书目录,进过一番摸索,使用WPS2009圆满完成了此次任务,愿与诸君共享. 1.显示大纲工具栏 打开“视图”——“工具栏”—— ...