题目链接: 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. Fork/Join框架介绍

    转http://www.infoq.com/cn/articles/fork-join-introduction/ 1. 什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用 ...

  2. RabbitMQ 四种Exchange

    AMQP协议中的核心思想就是生产者和消费者隔离,生产者从不直接将消息发送给队列.生产者通常不知道是否一个消息会被发送到队列中,只是将消息发送到一个交换机.先由Exchange来接收,然后Exchang ...

  3. 杂项:VS调试技巧之附加进程

    ylbtech-杂项:VS调试技巧之附加进程 1. 摘录返回顶部 1. 用过VS一段时间的程序员们相信都有过这种调试经历:每次按下F5进行断点调试时,都要等待好长时间:先让解决方式编译通过,然后启动V ...

  4. c语言-顺序表

    在数据结构中包含两种,一种线性结构(包括顺序表,链表,栈,队列),一种非线性结构(树,图), 顺序表,其实就是在内存动态数组,Java中的ArrayList就是一个典型的顺序表,它在顺序表的基础上增加 ...

  5. Oracle中REGEXP_SUBSTR函数(字符串转多行)

    Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20',' ...

  6. chrome开发者工具的使用

    转自:https://blog.csdn.net/csdnligao/article/details/53925094

  7. fontconfig

    vlc-android 默认是 禁用 fontconfig 的 如果想要使用的话需要手动修改 compile.sh

  8. 安卓读取SD卡的容量

    在开发中,我们经常会用到SD卡,那么在对SD卡进行读写的时候,我们经常需要判断SD卡的剩余容量是否足够.因此,这次我们来写写获取SD卡容量的程序. 该注意的地方,我都在程序里面有注明了.看程序基本就懂 ...

  9. java获取Linux持续运行时间及友好显示

    一.uptime命令 uptime命令可以查看系统的运行时间和负载 终端输入uptime 04:03:58 up 10 days, 13:19, 1 user, load average: 0.54, ...

  10. lombok与spring的恩怨

    下面是lombok按照 Java Bean 的规范生成的 下面是spring mvc里jackson 需要的 xXxx问题还是顺势而为吧