poj 1226 Substrings kmp 好题,我调试了一晚上啊!!汗
话说这道题我调试了5个小时,整整一晚上啊!!!!!!!!!!!!!!
说一下思路就是枚举第一个字符串所有的子串长度,去与其余的字符串kmp,如果都符合,那那个子串的长度就是答案!!!
#include<iostream>
using namespace std;
#define max 105
int next[max];
void prekmp(char aa[])
{
int j=-1;
next[0]=-1;
int len=strlen(aa);
for(int i=1;i<len;i++)
{
if(j>=0&&aa[i]!=aa[j+1])
j=next[j];
if(aa[i]==aa[j+1])
j++;
next[i]=j;
}
}
int kmp(char ss[],char a[])
{
// cout<<ss<<' '<<a;
// system("pause");
prekmp(ss);
int len=strlen(a);
int lens=strlen(ss);
// for(int i=0;i<lens;i++)
// cout<<next[i]<<' ';
// system("pause");
int j=-1;
// cout<<len;
// system("pause");
for(int i=0;i<len;i++)
{
// cout<<j<<' '<<lens<<' '<<a<<' '<<ss;
// system("pause");
if(j>=0&&ss[j+1]!=a[i])
j=next[j];
if(ss[j+1]==a[i])
j++;
// next[i]=j;//最关键的是我多加了这步,这里的next就是在子串ss数组里来回指,看j能不能知道子串ss数组末尾,而这步把j在next里指向a数组的范围了,所以加这部指定错!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// if(j==0)
// j=-1;
// cout<<j<<' '<<len;
// system("pause");
if(j==lens-1)
return 1;
}
return 0;
}
int main()
{
char a[max][max];
char ta[max],tb[max];
int t,n;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",a[i]);
int len=strlen(a[1]);
int sign=0,sign1=0;
int i;
for( i=len;i>=1;i--)
{
for(int j=0;j+i-1<len;j++)
{
int cou=0;
for(int k=j;k<=j+i-1;k++)
ta[cou++]=a[1][k];
ta[cou]='\0';
// for(int i=0;i<cou;i++)
// cout<<ta[i]<<' ';
// system("pause");
cou=0;
for(int k=j+i-1;k>=j;k--)
tb[cou++]=a[1][k];
tb[cou]='\0';
// cout<<ta;
// system("pause");
cou=0;
for(int k=2;k<=n;k++)
{
// system("pause");
if(kmp(ta,a[k])||kmp(tb,a[k]))
{
cou++;
}
}
if(cou==n-2+1)
{
// cout<<ta;
// system("pause");
sign=1;
sign1=1;
printf("%d\n",i);
break;
}
}
if(sign==1)
break;
}
if(sign1==0)
printf("0\n");
}
// system("pause");
return 0;
}
poj 1226 Substrings kmp 好题,我调试了一晚上啊!!汗相关推荐
- OpenJudge/Poj 1226 Substrings
1.链接地址: http://bailian.openjudge.cn/practice/1226/ http://poj.org/problem?id=1226 2.题目: 总时间限制: 1000m ...
- POJ - 1226 Substrings(后缀数组+二分)
题目链接:点击查看 题目大意:给出n个字符串,求出n个字符串中或者在他们翻转过来的字符串中,出现的最长公共子串的长度 题目分析:说白了就是求n个字符串中的最长公共子串的变形,而这个变形也相当简单,只是 ...
- POJ 3167 Cow Pattern ★(KMP好题)
题意 给你一个数字序列S,再给一个数字序列pattern,S和pattern中的数字都是1到s(s<=25).每个序列里的数字都有个排名,也就是第几小,现在我们要用pattern来匹配S.在本题 ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- poj-2406(kmp水题)
题意:定义一个a*b=字符串a连接字符串b:给你一个字符串s,问你这个字符串最多能用多少个字符串t连接得到:例如:aaaa=4个a构成: 解题思路:kmp水题,next数组除了查找字串以外最广泛的一种 ...
- Oulipo(欧力波)(经典kmp模板题) HDU-1686
题目:Oulipo(欧力波) 中文大意 The French author Georges Perec (1936�C1982) once wrote a book, La disparition, ...
- hihoCoder 1015 (KMP模板题)
题目链接:http://hihocoder.com/problemset/problem/1015 Time Limit:1000ms Case Time Limit:1000ms Memory Li ...
- POJ:3461-Oulipo(KMP模板题)
原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...
- POJ - 3461 (kmp)
题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- POJ 3030 Nasty Hacks (模拟题)
题目:http://poj.org/problem?id=3030 思路:模拟题 #include <iostream>using namespace std;int main() {in ...
最新文章
- [转载]如何发送和接收 Windows Phone 的 Toast 通知
- python3入门-终于懂得python3快速入门教程
- Linux下epoll如何实现高效处理百万句柄的
- 160个Crackme035
- c调用按钮点击事件_React中事件的写法总结
- vsftp虚拟用户权限问题
- spark sortBy sortByKey实战详解
- python日志模块logging
- 巴特沃斯(Butterworth)滤波器的设计和幅频特性曲线绘制
- 大气压随温度变化表_空气密度随温度的变化
- 使用SQL语句在K3里进行反结帐- -
- 投入产出表matlab,投入产出分析投入产出表.doc
- 150. 逆波兰表达式求值(中等 栈 数组)
- 曲率发动机和反重力装置原理
- Unable to find libthread_db matching inferior‘s thread library, thread debugging will not be availab
- FileNotFoundError: [Errno 2] No such file or directory: ‘ETHZ/eth01/images/image_00000001_2021-10-29
- C++反汇编代码分析--函数调用
- P2P之UDP穿透NAT的原理与实现(附源代码)
- Perl split函数
- Python定时任务框架APScheduler快速入门
热门文章
- BootStrap_04之jQuery插件(导航、轮播)、以及Less
- flask开发restful api系列(7)-蓝图与项目结构
- windows中squid更改默认安装路径配置说明
- UDP穿越NAT原理(p2p)
- codeigniter中base_url和site_url
- 一个连衣服都穿不整齐的人,代码也肯定写不整齐。
- 《单细胞生物》教学反思
- appium连接真机时,报错:error: device unauthorized.
- pip常用命令、配置pip源
- 认识文本和文本文件 字符流操作的是文本文件