题目链接: https://vjudge.net/contest/70325#problem/I

题意: 求多个字符串的最长公共子串, 有多个则输出字典序最小的.

思路: 这里的字符串长度固定为 60, 可以枚举其中一个字符串的所有子串, 然后拿这个子串去和其他字符串匹配就好了. 不过如果不用 kmp 的话需要 O(N^5) 的时间复杂度, 因该会 tle.

代码:

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int MAXN = 1e2;
int len, nxt[MAXN];
char s[MAXN], sol[MAXN], str[][MAXN]; void get_nxt(void){
memset(nxt, , sizeof(nxt));
for(int i = ; i < len; i++){
int j = nxt[i];
while(j && s[i] != s[j]){
j = nxt[j];
}
nxt[i + ] = j + (s[i] == s[j]);
}
} bool kmp(char *gel){
for(int i = , j = ; i < strlen(gel); i++){
while(j && gel[i] != s[j]){
j = nxt[j];
}
if(gel[i] == s[j]) j++;
if(j >= len) return true;
}
return false;
} int main(void){
int t, n;
scanf("%d", &t);
while(t--){
int cnt = ;
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%s", str[i]);
}
for(int i = ; i < ; i++){
len = ;
bool flag = true;
s[len++] = str[][i];
s[len++] = str[][i + ];
for(int j = i + ; j < ; j++){
s[len++] = str[][j];
s[len] = '\0';
get_nxt();
for(int k = ; k < n; k++){
flag = kmp(str[k]);
if(!flag) break;
}
if(!flag) break;
else{
if(j - i + > cnt){
cnt = j - i + ;
strcpy(sol, s);
}else if(j - i + == cnt){
if(strcmp(sol, s) > ) strcpy(sol, s);
}
}
}
}
if(cnt < ) puts("no significant commonalities");
else printf("%s\n", sol);
}
return ;
}

kuangbin专题16I(kmp)的更多相关文章

  1. kuangbin专题16B(kmp模板)

    题目链接: https://vjudge.net/contest/70325#problem/B 题意: 输出模式串在主串中出现的次数 思路: kmp模板 在 kmp 函数中匹配成功计数加一, 再令 ...

  2. kuangbin专题16A(kmp模板)

    题目链接: https://vjudge.net/contest/70325#problem/A 题意: 有两个数组 a, b, 输出 b 数组在 a 数组中的第一个匹配位置, 不能匹配则输出 -1. ...

  3. [kuangbin]专题六 最小生成树 题解+总结

    kuangbin专题链接:https://vjudge.net/article/752 kuangbin专题十二 基础DP1 题解+总结:https://www.cnblogs.com/RioTian ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  5. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

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

  6. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  8. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

随机推荐

  1. Windows Server 2008 修改系统的SID

    故事背景:用VMware搭建了几个操作系统相同的虚拟机.安装成功一台后,直接拷贝已经生成的VMDK文件来构建其它的虚拟机. 一般情况下,如果复制的各个虚拟机只是单独使用,并且这些虚拟机不加入到域(Ac ...

  2. 几种排序方式的java实现(02:希尔排序,归并排序,堆排序)

    代码(部分为别人代码): 1.希尔排序(ShellSort) /* * 希尔排序:先取一个小于n的整数d1作为第一个增量, * 把文件的全部记录分成(n除以d1)个组.所有距离为d1的倍数的记录放在同 ...

  3. 机器学习:集成学习(Ada Boosting 和 Gradient Boosting)

    一.集成学习的思路 共 3 种思路: Bagging:独立的集成多个模型,每个模型有一定的差异,最终综合有差异的模型的结果,获得学习的最终的结果: Boosting(增强集成学习):集成多个模型,每个 ...

  4. Java-API:java.util.ArrayList

    ylbtech-Java-API:java.util.ArrayList 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://docs.orac ...

  5. 【转】Rails中Bootstrap的安装和使用

     转自:http://blog.csdn.net/lissdy/article/details/9195651   眼看着前端攻城师们都开始使用Bootstrap创作网页,于是也想学着在最近正在学习的 ...

  6. c++ 端口扫描程序

    第一.原理 端口扫描的原理很简单,就是建立socket通信,切换不通端口,通过connect函数,如果成功则代表端口开发者,否则端口关闭. 所有需要多socket程序熟悉,本内容是在window环境下 ...

  7. HDLM命令dlnkmgr详解之三__view

    view命令主要用于显示program information, path information, LU information, HBA port information, CHA port in ...

  8. javascript——对象的概念——创建对象与销毁对象

    一.创建对象 1.创建空对象 方式一: var o ={};o; //Object {} typeof(o); //"object" 方式二: var o=new Object() ...

  9. 在ACCESS中LIKE的用法

    Access里like的通配符用法是这样:     “?”表示任何单一字符: “*”表示零个或多个字符: “#”表示任何一个数字     所以应该是:     select * from databa ...

  10. Struts旅程(六)Struts页面转发控制ActionForward和ActionMapping

    转自:https://blog.csdn.net/lovesummerforever/article/details/19125933