题意:

给定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+暴搞)的更多相关文章

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

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

  2. POJ 3450 Corporate Identity kmp+最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  3. POJ 3450 Corporate Identity(KMP)

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

  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 题目3450 Corporate Identity(KMP 暴力)

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 201 ...

  7. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  8. hdu 2328 Corporate Identity(kmp)

    Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...

  9. POJ 3450 后缀数组/KMP

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

随机推荐

  1. leetcodequestion_56 Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  2. oracle之时间转换

    :取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; TO_CHAR(SYSDATE,'Y ...

  3. sublime3 ctl+b无效

    Preference->Browse Packages->python,编辑Python.sublime-build文件,在字典里添加path item,value为你的python路径, ...

  4. web标准(复习)--1

    XHTML CSS基础知识 一.xhtml css基础知识首先说一下我们这节课的知识点 1.文档类型 2.语言编码 3.html标签 4.css样式 5.css优先级 6.css盒模型组成 1)文档类 ...

  5. Activiti工作流学习-----基于5.19.0版本(3)

    前面关于eventType的属性值的配置简单的说了一下,activiti支持的值如下表所示:这是我摘抄的activiti官网的 Event 的名字 描述 Event的类名 ENGINE_CREATED ...

  6. jQuery Mobile 网格

    在列容器中,根据不同的列数,子元素可设置类 ui-block-a|b|c|d|e.这些列将依次并排浮动. 网格中的列是等宽的(总宽是 100%),无边框.背景.外边距或内边距. 例: 对于 ui-gr ...

  7. Caffe--solver.prototxt配置文件 参数设置及含义

    ####参数设置################### 1. ####训练样本### 总共:121368个batch_szie:256将所有样本处理完一次(称为一代,即epoch)需要:121368/ ...

  8. web.xml 3.0头部模板

    <?xml version=”1.0″ encoding=”UTF-8″?><web-appversion=”3.0″xmlns=”http://java.sun.com/xml/n ...

  9. hdu 七夕节

    #include <cstdio> #include <cstring> #include <algorithm> #define maxn 500000 usin ...

  10. VS2010编译Qt程序失败------error LNK1123: 转换到 COFF 期间失败:

    error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏使用VS2010编译VC++项目的时候可能会出这个问题. 据说升级到SP1后可能问题解决,但是下载量太大,目前没有得到证实. ...