POJ 3450 Corporate Identity (KMP+暴搞)
题意:
给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个。
找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
char str[4111][221];
int next[222]; void getnext(char *t) {
int i=0,j=-1;
int len = strlen(t);
next[0] = -1;
while(i < len) {
if(t[i] == t[j] || j == -1) {
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=0,j=0;
while(i < lens && j < lena) {
if(s[i] == t[j] || j == -1) {
i++;
j++;
} else j = next[j];
}
if(j < lena) return -1;
return i - lena;
} int main() {
int n,len;
while(cin >> n && n) {
char tmp[222];
int minn = 111111;
for(int i=0; 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[222];
char final[222] = {0};
int ans = 0;
for(int i=1; i<=len; i++) { //枚举所有的字串
int cnt;
for(int j=0; j + i<=len; j++) {
cnt = 0;
strncpy(p,tmp+j,i);
p[i] = '\0'; getnext(p);
for(int k=0; k<n; k++) { //逐个比较
if(kmp(str[k],p) != -1) {
cnt ++;
}
else break;
}
if(cnt == n) {
ans++; if(strlen(final) < strlen(p)) strcpy(final,p);
else if(strcmp(final,p) > 0) strcpy(final,p); }
}
}
if(ans == 0) printf("IDENTITY LOST\n");
else {
printf("%s\n",final);
}
}
return 0;
}
POJ 3450 Corporate Identity (KMP+暴搞)的更多相关文章
- POJ 3450 Corporate Identity KMP解决问题的方法
这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- 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 题目3450 Corporate Identity(KMP 暴力)
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493 Accepted: 201 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)
刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...
- MySQL数据库中的哈希加密
数据库安全是数据库中最为重要的环节,只有确保了数据库中数据的安全,才能够更好的发挥数据库的功能,本文将为大家介绍一种很好的数据库加密方法,即哈希加密. 导读:MySQL数据库加密的方法有很多种,不同的 ...
- vs开发常用快捷键
Ctrl+K+D:快速对齐代码///按D的时候K快速弹起 如果出现语法错误则无法对齐Ctrl+Z:撤销Ctrl+S:保存Ctrl+J:快速弹出智能提示Shift+End.Shift+Home//快速选 ...
- textarea固定大小,不可拖动
写前端,经常很多小东西容易忽略忘记,今天写页面碰到设定一个输入框大小,死活记不起怎么固定,故找了一下度娘,其实添加一个css属性就好了: resize: none; 随笔记一下!
- (转)回车 执行button点击
功能说明:当我们注册或者执行搜索时,输入内容后,不用单击按钮来执行按钮单击事件,而直接通过回车来执行按钮单击事件.只要在text框中onkeydown事件中加入执行按钮的onclick事件就OK了 代 ...
- ios9基础知识总结(foundation)笔记
类:NSObject .NSString.NSMutableString.NSNumber.NSValue.NSDate.NSDateFormatter.NSRange.Collections:NSS ...
- JDK6和JDK7中的substring()方法
substring(int beginIndex, int endIndex)在JDK6与JDK7中的实现方式不一样,理解他们的差异有助于更好的使用它们.为了简单起见,下面所说的substring() ...
- 解读CSS的背景(background)样式
background-color: 可以为所有的元素设置背景色,这个属性接受任意合法的颜色值,如果希望背景色从元素文本向外少有延伸,只需增加一些内边距(padding). 注意:background- ...
- 作业:汽车查询--弹窗显示详情,批量删除 php做法(0521)
作业:显示以下界面: 作业要求: 1.查看详细信息,以弹窗的形式显示,使用ajax2.批量删除 一.主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...
- .NET winform 在listview中添加progressbar
找了好长时间没找到,后来索性自己写了一个: 首先,在往listview加载数据的事件里添加progressbar: foreach (string d in arr) { ; item = new L ...