这个问题,需要一组字符串求最长公共子,其实灵活运用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. 解决sdk manager无法更新的问题

    我是在mac下安装了android studio,没有sdk manager,于是下了单独的sdk manager,勾选了想要下载的内容,但总是出现"nothing installed&qu ...

  2. Android studio ElasticDownloadView

    找到个开源项目,地址:https://github.com/Tibolte/ElasticDownload 下载进度效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...

  3. Android学习4、Android该Adapter

    一.Adapter介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for th ...

  4. 新手推荐:IIS+PHP+MYSQL环境配置教程

    本文介绍刚开始接触php的朋友如何为自己的服务器配置php环境 首先我们要的工具: 1.IIS:这个当然是不能少的了,用系统自带的就好了,这里就不教大家怎么装了. 2.PHP:php-5.2.0-wi ...

  5. UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)

    继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...

  6. 调整Tomcat的并发线程到5000+

    调整Tomcat的并发线程数到5000+ 1. 调整server.xml的配置 先调整maxThreads的数值,在未调整任何参数之前,默认的并发线程可以达到40. 调整此项后可以达到1800左右. ...

  7. struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值

    在Struts2中,一个请求在终于到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是 ...

  8. ThinkPHP 的模型使用详细介绍--模型的核心(七)

    原文:ThinkPHP 的模型使用详细介绍--模型的核心(七) 注意:本节是ThinkPhp框架对数据操作的核心处理部分 大家还是在这里看清楚可以将其剪切放到代码编辑器中查看 本章节给大家着重介绍模型 ...

  9. [Xcode]some little skill

    Date:2014-1-2 Summary: 自己在使用Xcode的一些小习惯,记录下来,我是这么用的,你呢? Contents:1.使用#warning 在工作中,难免需要做一些test,但是又怕忘 ...

  10. sort 使用 tab键 作为 分隔符_人生如梦_百度空间

    sort 使用 tab键 作为 分隔符_人生如梦_百度空间 sort 使用 tab键 作为 分隔符 For some reason "\t" doesn't work right, ...