题目链接: 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. springboot+springcloud config

    参考:sorry,全找不到了,当时没记录,最后后知后觉觉得应该记录,所以后面的都有在asfood父项目中的doc文件夹下记录,望见谅. 1. springconfig server 1.1. pom. ...

  2. (转)C# 特性(Attribute)详细介绍

    本文转载自:http://www.cnblogs.com/luckdv/articles/1682488.html 1.什么是Atrribute 首先,我们肯定Attribute是一个类,下面是msd ...

  3. python第二十三天-----Tornado

    Tornado是一个轻量级完整的web框架,在Linux系统下它会使用epoll,是一个异步非阻塞的web服务器框架,对于实时应用来说很理想,想想同是异步非阻塞的nginx的残暴程度就知道了 1.路由 ...

  4. “百度杯”CTF比赛 2017 二月场(Misc Web)

    爆破-1: 打开链接,是502 我直接在后面加个变量传参数:?a=1 出了一段代码 var_dump()函数中,用了$$a,可能用了超全局变量GLOBALS 给hello参数传个GLOBALS 得到f ...

  5. 新建 FrameMaker API 时引用目录的设置

    如果将FDK安装目录下的Sample项目拷贝到其它目录编译,往往会报错 c1083 找不到fapi.h等头文件,或者Link时报错找不到.lib文件. 1.可通过菜单-项目-xxx属性-配置属性-c/ ...

  6. 第四章 Javac编译原理(待续)

    Javac是什么 Javac编译器的基本结构 Javac工作原理分析 设计模式解析之访问者模式

  7. How to clear fmadm log or FMA faults log (ZT)

    Here are the step by step of clearing the FMA faults on most of Oracle/Sun server. Work perfectly on ...

  8. 10-12C#基础--运算符

    10-12C#基础--运算符 课前作业:班级内人数的姓名和年龄,分别写出之后并汇总. 一.运算符的分类 1.数学运算符(7个) 1)+(加号) 例: 2)-(减号) 例: 3)*(乘号) 例: 4)/ ...

  9. LNMP 1.5 php-fpm配置文件

    php-fpm配置文件: /usr/local/php/etc/php-fpm.conf    :php-fpm服务的配置文件 /usr/local/php/etc/php.ini       :ph ...

  10. .net 4 安装未成功,无意中的解决办法!

    公司 电脑是chost的系统,由于使用时间过长,重装纯净版系统的话,代价太大,故网上寻求各种解决办法! 安装.net 4 总是失败,查看百度,各种: WIN7系统哈哈跟我的问题一样,我的刚才解决了:1 ...