题意:给你n个字符串,问你这里面最长的公共子串的长度是多少,一个公共子串的反串也算,比如样例二;

解题思路:随便找一个字符,枚举它的子串然后跑kmp就行了,很多人的博客都是用string类里面的函数来解决的,学到了。。。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define maxn 205
using namespace std;
char x[maxn][maxn];
char str[maxn];
char str2[maxn];
int next1[maxn];
int cnt,cot;
void get_next(char *t)
{
int j,k;
int tlen=cnt;
j=0;k=-1;next1[0]=-1;
while(j<tlen)
{
if(k==-1||t[j]==t[k])
next1[++j]=++k;
else
k=next1[k];
}
}
int kmp_pos(char *t,char *s)
{
int slen=strlen(s);
int tlen=cnt;
int i=0,j=0;
get_next(t);
while(i<slen&&j<tlen)
{
if(j==-1||s[i]==t[j])
{
i++;j++;
}
else
j=next1[j];
}
if(j==tlen)
return i-tlen;
return -1; }
int main()
{
int tt;
int n;
int ans;
int maxx;
int l;
scanf("%d",&tt);
while(tt--)
{
maxx=-1;l=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",x[i]);
int m=strlen(x[1]);
for(int i=0;i<m;i++)
{
cnt=0;
for(int j=i;j<m;j++)
{
ans=0;cot=0;
str[cnt++]=x[1][j];
for(int k=cnt-1;k>=0;k--)
str2[cot++]=str[k];
/*for(int k=0;k<cnt;k++)
cout<<str[k];
cout<<endl;
for(int k=0;k<cnt;k++)
cout<<str2[k];
cout<<endl;*/
for(int k=1;k<=n;k++)
{
int flag1=kmp_pos(str,x[k]);
int flag2=kmp_pos(str2,x[k]);
if(flag1==-1&&flag2==-1)
continue;
ans++; }
if(ans==n)
{
l=max(l,cot);
}
}
}
printf("%d\n",l); }
}

  

hdu-1238(kmp+枚举)的更多相关文章

  1. (KMP 字符串处理)Substrings -- hdu -- 1238

    http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit:1000MS     Memory Limit:32768KB ...

  2. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  3. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  4. hdu 1238 Substrings(kmp+暴力枚举)

    Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...

  5. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  6. HDOJ(1238) KMP

    Substrings http://acm.hdu.edu.cn/showproblem.php?pid=1238 先找到长度最短的字符串,把它的子串和该子串的逆序(按长度从大到小)依次与其他字符串匹 ...

  7. hdu 2489(枚举 + 最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 思路:由于N, M的范围比较少,直接枚举所有的可能情况,然后求MST判断即可. #include ...

  8. hdu 3118(二进制枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...

  9. HDU 2087 (KMP不可重叠的匹配) 花布条

    题意: 用两个字符串分别表示布条和图案,问能从该布条上剪出多少这样的图案. 分析: 毫无疑问这也是用KMP匹配,关键是一次匹配完成后,模式串应该向后滑动多少. 和上一题 HDU 1686 不同,两个图 ...

  10. Substrings - HDU 1238(最大共同子串)

    题目大意:给你N个串,求出来他们的最大公共子串的长度(子串反过来也算他们的子串).   分析:很久以前就做过这道题,当时是用的strstr做的,不过相同的都是枚举了子串......还是很暴力,希望下次 ...

随机推荐

  1. c#简单的io

    读取路径判断文件是否存在,进行删除或者创建 简单的io using System; using System.Collections; using System.Collections.Generic ...

  2. mysql基本知识点梳理和查询优化

    目录 一.索引相关 二.EXPLIAN中有用的信息 三.字段类型和编码 四.SQL语句总结 五.踩坑 六.千万大表在线修改 七.慢查询日志 八.查看sql进程和杀死进程 九.一些数据库性能的思考 本文 ...

  3. C#_IO操作_查询指定文件夹下的每个子文件夹占空间的大小

    1.前言 磁盘内存用掉太多,想查那些文件夹占的内存比较大,再找出没有用的文件去删除. 2.代码 static void Main(string[] args) { while (true) { //指 ...

  4. SQL Server 分析函数和排名函数

    分析函数基于分组,计算分组内数据的聚合值,经常会和窗口函数OVER()一起使用,使用分析函数可以很方便地计算同比和环比,获得中位数,获得分组的最大值和最小值.分析函数和聚合函数不同,不需要GROUP ...

  5. Groovy语言学习--语法基础(2)

    集合和闭包 因为之前没接触过C++等,对指针也一窍不通.个人不成熟的了解 闭包是一种数据类型,可以很方便的执行一段独立的代码 简化方法的调用 package groovy /** * Groovy容器 ...

  6. ASP.NET Core 企业开发架构概述

    企业开发框架包括垂直方向架构和水平方向架构.垂直方向架构是指一个应用程序的由下到上叠加多层的架构,同时这样的程序又叫整体式程序.水平方向架构是指将大应用分成若干小的应用实现系统功能的架构,同时这样的系 ...

  7. 【redis】windows 怎样关闭redis

    安装redis之后在命令行窗口中输入 redis-server redis.windows.conf 启动redis关闭命令行窗口就是关闭 redis.---redis作为windows服务启动方式r ...

  8. 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)

    lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...

  9. 出题人的女装(牛客练习赛38题B) (概率+分式运算)

    链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  10. python3 常见的两种文件上传方法

    1.上传页面带input type格式send_keys传值方式上传不能大于60k(具体看开发设置的value)文件大小 fx.find_element_by_id('xx').send_keys(r ...