POJ 3450 Corporate Identity kmp+最长公共子串
#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+最长公共子串的更多相关文章
- POJ 3450 Corporate Identity KMP解决问题的方法
这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- POJ 2774 Long Long Message [ 最长公共子串 后缀数组]
题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total ...
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- POJ 3450 Corporate Identity (KMP+暴搞)
题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
- POJ3450最长公共子串【kmp】
题目链接:http://poj.org/problem?id=3450 题目大意:给定n个长度不超过200的字符串,n < 4000.求这些字符串的最长公共子串,若没有,则输出 “IDENTIT ...
随机推荐
- IOS中利用宏将RGB值转换为UIColor(转)
可以在pch文件中定义宏,这样整个项目就都可以用了! #define UIColorFromRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgb ...
- 仿window阿里旺旺登陆界面,打印机吐纸动画效果-b
偶然的机会发现window的阿里旺旺的登陆效果蛮有意思的,于是就模仿着做了一下打印机吐纸的动画效果看起来很神奇的东西,实现起来却不难,下面我给大家看下主要的源码. - (void)createUI{ ...
- Python标准库与第三方库详解(转载)
转载地址: http://www.codeweblog.com/python%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%8e%e7%ac%ac%e4%b8%89%e6%96%b ...
- push 栈顶sp=sp-2 可以把立着的栈,向左侧倒下,那么形态就和反汇编时,内存的形态是一样的。小偏移的字节在前, 大的偏移字节在后
push 栈顶sp=sp-2 可以把立着的栈,向左侧倒下,那么形态就和反汇编时,内存的形态是一样的.小偏移的字节在前, 大的偏移字节在后. 1 2 3 4 5 1 2 3 4 5
- [Gauss]POJ2065 SETI
题意: *代表0,a-z代表1-26 题目第三行给了一个公式 f (k) = $\sum\limits_{i=0}^{n-1} a_i k^i \pmod{P}$ {f(i)是输入的一串字符串中第i ...
- HDOJ多校联合第六场
先一道一道题慢慢补上, 1009.题意,一棵N(N<=50000)个节点的树,每个节点上有一个字母值,给定一个串S0(|S0| <=30),q个询问,(q<=50000),每次询问经 ...
- 李洪强iOS开发之提交AppStory时候遇到的坑
今天我在上传AppStore的时候,遇到了很多的问题.一直找不到问题的原因,但是最后终于发现问题的原因 ,是因为钥匙串签名无效的问题,解决方案如下: 证书签名无效解决: 1,按照你那个链接下载,htt ...
- Java集合类之Hashtable
package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...
- Java二维数组
package com.test; public class Test { public static void main(String[] args) { // TODO Auto-generate ...
- RxJava开发精要4 – Observables过滤
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...