kuangbin专题16I(kmp)
题目链接: 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)的更多相关文章
- kuangbin专题16B(kmp模板)
题目链接: https://vjudge.net/contest/70325#problem/B 题意: 输出模式串在主串中出现的次数 思路: kmp模板 在 kmp 函数中匹配成功计数加一, 再令 ...
- kuangbin专题16A(kmp模板)
题目链接: https://vjudge.net/contest/70325#problem/A 题意: 有两个数组 a, b, 输出 b 数组在 a 数组中的第一个匹配位置, 不能匹配则输出 -1. ...
- [kuangbin]专题六 最小生成树 题解+总结
kuangbin专题链接:https://vjudge.net/article/752 kuangbin专题十二 基础DP1 题解+总结:https://www.cnblogs.com/RioTian ...
- 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 ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- 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 ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
随机推荐
- struts2获得需要的文件或者访问路径
在struts2中,上传文件的时候遇到一个很好用但是失效的方法,找到如下替代.并且测试了一下request可以得到的相关路径. 得到request对象: HttpServletRequest requ ...
- java 多线程系列基础篇(三)之start()和run()的区别
概要 Thread类包含start()和run()方法,它们的区别是什么?本章将对此作出解答. start() 和 run()的区别说明 start() : 它的作用是启动一个新线程,新线程会执行相应 ...
- 用JS,打印99乘法表
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- springmvc 注解式开发 接收请求参数
1.校正请求参数名: 2.以对象形式整体接收 3.路径变量:
- Android广播接收者
其实,在什么是广播的第一句就已经说明了广播有什么用了.对了,笼统一点讲就是用来传输数据的.具体一点说就是:1. 实现了不同的程序之间的数据传输与共享,因为只要是和发送广播的action相同的接受者都能 ...
- JavaScript的作用域与闭包
JavaScript的作用域以函数为界,不同的函数拥有相对独立的作用域.函数内部可以声明和访问全局变量,也可以声明局部变量(使用var关键字,函数的参数也是局部变量),但函数外部无法访问内部的局部变量 ...
- Swing的MVC结构
--------------siwuxie095 工程名:TestMVC 包名:com.siwuxie095.mvc 类名:Test.java ...
- SQl Server Tsql基本编程 ,循环语句 ,存储过程
一些比较重要但是不一定经常用的 句子 Tsql定义变量 declare @a int : 定义的变量前面必须用@,数据类型是SQL里的数据类型,执行的时候要把需要的有关联的代码一起执行,单独执行一条 ...
- SimpleDateFormat-多线程问题
SimpleDateFormat-多线程问题: SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况 import java.text.ParseException; ...
- JS对表单的操作
JS对表单中的style的操作,包括复选框技术 废话不多说直接上文件代码!!! 功能:全选\反选,鼠标监测变颜色 <html> <head> <meta charset= ...