这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀。

请注意,意大利愿父亲:按照输出词典的顺序的规定。

另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用的暴力方法,没有真正处理的细节。发挥KMP角色。而通常这些人都大喊什么暴力法能够解决本题,没错,的确暴力法是能够解决本题的,本题的数据不大,可是请不要把KMP挂上去,然后写成暴力法了。那样会误导多少后来人啊。

建议能够主要參考我的getLongestPre这个函数,看看是怎样计算最长前缀的。

怎么推断你是否把本题的KMP法写成暴力法了?

简单看看你的执行速度吧。我的以下的程序执行起来是79ms(每次执行或许有一点点区别)。假设你搞出KMP执行时间300ms,甚至搞得更加高了,看到号称使用KMP搞出700多ms执行时间的,囧,那么说明你还不会KMP,你挂了个KMP外壳,写成了暴力法了。

#include <stdio.h>
#include <string.h> const int MAX_N = 4001;
const int MAX_CHS = 201;
const int ARR_SIZE = 26;
int N;
char res[MAX_CHS], dict[MAX_N][MAX_CHS];
short next[MAX_CHS]; inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; } void getNext(char *chs, int len)
{
memset(next, 0, sizeof(short)*len);
for (int i = 1, j = 0; i < len; )
{
if (chs[i] == chs[j]) next[i++] = ++j;
else if (j > 0) j = next[j-1];
else i++;
}
} int getLogestPre(char *chs, int len)
{
getNext(chs, len);
for (int i = 1; i < N; i++)
{
char *p = dict[i];
int j = 0, tmp = 0;
for (; *p && j < len; )
{
if (*p == chs[j])
{
p++, j++;
tmp = max(tmp, j);
}
else if (j > 0) j = next[j-1];
else p++;
}
len = tmp;
}
return len;
} int main()
{
while (scanf("%d", &N) && N)
{
getchar(); // don't forget to get rid of '\'
for (int i = 0; i < N; i++)
{
gets(dict[i]);
}
int len = strlen(dict[0]), ans = 0, pos = 0;
for (int i = 0; i < len; i++)
{
int tmp = getLogestPre(dict[0]+i, len-i);
if (tmp >= ans)
{
if (tmp > ans)
{
ans = tmp;
pos = i;
}
else
{
bool smaller = true;
for (int t = 0; t < ans; t++)
{
if (dict[0][pos+t] > dict[0][i+t]) break;
else if (dict[0][pos+t] < dict[0][i+t])
{
smaller = false;
break;
}
}
if (smaller) pos = i;
}
}
}
if (ans)
{
for (int i = 0; i < ans; i++)
{
putchar(dict[0][pos+i]);
}
putchar('\n');
}
else puts("IDENTITY LOST");
}
return 0;
}

POJ 3450 Corporate Identity KMP解决问题的方法的更多相关文章

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

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

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

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

  3. POJ 3450 Corporate Identity(KMP)

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

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

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

  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. HADOOP之MAPREDUCE程序应用二

    摘要:MapReduce程序进行单词计数. 关键词:MapReduce程序  单词计数 数据源:人工构造英文文档file1.txt,file2.txt. file1.txt 内容 Hello   Ha ...

  2. 应用程序初始化正常(0xc015002)失败解决方法

    VS2005 sidebyside manifest error Microsoft.VC80.MFC Microsoft.VC80.CRT Microsoft.VC80.MFCLOC msvcr80 ...

  3. 基于飞思卡尔i.MX 6Quad Sabrelite开发板的触摸屏调试

    1      概述 本次任务是在飞思卡尔i.MX 6Quqd Sabrelite开发板上调试触屏驱动,触屏芯片是Goodix的gt828芯片,触屏接口是I2C. 操作系统:android 4.0.4 ...

  4. Codechef Not a Triangle题解

    找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...

  5. shodan

    https://www.shodan.io/ from:http://www.exploit-db.com/wp-content/themes/exploit/docs/33859.pdf 0x00 ...

  6. phing用户手册第四章Getting Started译文

    本章是phing的入门篇,查看 原文请猛击这里. XML And Phing 一个合法的Phing构建文件有以下几部分构成: 1.文档序言 2.唯一的根元素<project> 3.一些Ph ...

  7. postgresql优化数据的批量插入

    原文:http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 有以下几种方法用于优化数据的批量插入. 1. 关闭自动提交:      ...

  8. Java程序猿面试题集(181- 199)

    Java面试题集(181-199) 摘要:这部分是包括了Java高级玩法的一些专题,对面试者和新入职的Java程序猿相信都会有帮助的. 181.  182. 183. 184. 185. 186. 1 ...

  9. Accord.Net中决策树

    Accord.Net中决策树 决策树介绍 决策树是一类机器学习算法,可以实现对数据集的分类.预测等.具体请阅读我另一篇博客(http://www.cnblogs.com/twocold/p/54245 ...

  10. poj2245Lotto(最基础的dfs)

    题目链接: 啊哈哈,点我点我 思路:最開始画好搜索状态,然后找好结束条件,最好预推断当前找到的个数和能够找到的是否大于6就可以.. 题目: Lotto Time Limit: 1000MS   Mem ...