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, ...
随机推荐
- SQL子查询/嵌套查询
sql子查询 嵌套SELECT语句 嵌套SELECT语句也叫子查询,一个 SELECT 语句的查询结果能够作为另一个语句的输入值.子查询不但能够出现在Where子句中,也能够出现在from子句中,作为 ...
- boost的asio接收单路大数据量udp包的方法
开发windows客户端接收RTP视频流,当h264视频达到1080P 60fps的时候,按包来调用recvfrom的函数压力比较大,存在丢包的问题,windows的完成端口的性能效果当然可以解决这个 ...
- Oracle 12c 搭建学习
Oracle 12c 搭建学习 Vm workstaton10 安装linux 6.4 安装oracle12c Oracle 12c只支持64位系统 1 环境检查 [root@rac1 ~]# gre ...
- 使用like查询text类型字段
使用like查询text类型字段 public bool Exists(GetReadType GRT, ClientMessageGetRead TypeID, string MessageID, ...
- 跨resetlogs不完全恢复- oracle自动reset错误incarnation
在做oracle跨resetlogs的不完全恢复时,如果未删除flashback area的控制文件自动备份,那么Oracle可能会从控制文件自动备份中自动探测到incarnation信息,并重置in ...
- JavaScript语言基础-对象与数组
- Replace Pioneer注册方法
Replace Pioneer注册方法 Replace Pioneer过期后,会弹出一个注册(Registration)窗口,其中有一个试用选项(Trial License),点击Trial Lice ...
- [cerc2017J]Justified Jungle
题目大意:删去k条边,树变为相等个点的连通分量,求所有正整数k. 解题关键:树dp,不必求因子. #include<bits/stdc++.h> using namespace std; ...
- c++的单例模式及c++11对单例模式的优化
单例模式 单例模式,可以说设计模式中最常应用的一种模式了,据说也是面试官最喜欢的题目.但是如果没有学过设计模式的人,可能不会想到要去应用单例模式,面对单例模式适用的情况,可能会优先考虑使用全局或者静态 ...
- Emgu cv3.0.0 图像收集
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...