这个问题,需要一组字符串求最长公共子,其实灵活运用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. c#indexof使用方法

    IndexOf() 查找字串中指定字符或字串首次出现的位置,返首索引值,如: str1.IndexOf("字"): //查找"字"在str1中的索引值(位置) ...

  2. Swift - 告警提示框(UIAlertController)的用法

    自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView.下面总结了一些常见的用法: 1,简单的应用(同时按钮响应Handler使用闭包函数)    1 2 3 ...

  3. TCP三次握手和Time-Wait状态

    第一次握手:建立连接时.client发送syn包和一个随机序列号seq=x到server,并进入SYN_SEND状态,等待server进行确认. (syn,同 步序列编号). 第二次握手,server ...

  4. java.lang.ClassCastException: sun.proxy.$Proxy11 cannot be cast to分析

    报这个错,只有一个原因,就是你转化的类型不对. 如果你的类是一个单实体类,也就是没有继承或是接口别的类. public class HjmServiceImpl {} 那么这样写就可以: HjmSer ...

  5. Oracle 验证IOT表数据存储在主键里

    iot表测试: 在create table语句后面使用organization index,就指定数据表创建结构是IOT.但是在不指定主键Primary Key的情况下,是不允许建表的. create ...

  6. 搜索引擎爬虫蜘蛛的USERAGENT大全

    搜索引擎爬虫蜘蛛的USERAGENT大全 搜索引擎爬虫蜘蛛的USERAGENT收集,方便制作采集的朋友.   百度爬虫 * Baiduspider+(+http://www.baidu.com/sea ...

  7. liGDX life_cycle (生命周期)

    本文章翻译自libGDX官方wiki,,转载请注明出处:http://blog.csdn.net/kent_todo/article/details/37940489 libGDX官方网址:http: ...

  8. HDU 4597 Play Game 2013 ACM-ICPC吉林通化全国邀请赛H题

    九野的博客,转载请注明出处:  http://blog.csdn.net/acmmmm/article/details/10833941 题意:给定T个测试数据,下面有2副牌,每副n张,每张都有一个分 ...

  9. GMM的EM算法

    在聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut一文中我们给出了GMM算法的基本模型与似然函数,在EM算法原理中对EM算法的实现与收敛性证明 ...

  10. RANSAC - 随机采样一致性算法

    RANSAC范例的正式描述如下: 首先,要给定: 1一个模型,该模型需要最少n个数据点去实例化它的自由参数: 2一组数据点P,P中包含数据点的数量#(P)大于n. 然后, 从P中随机地选择n个点(组成 ...