KMP算法2
来源:互联网 发布:豆瓣和知乎的区别 编辑:程序博客网 时间:2024/06/16 02:49
//hdu 1867
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX=100009;
char str1[MAX],str2[MAX];
int next_[MAX];
void getNext(char *pattern)
{
int i=0,j=-1;
next_[0]=-1;
int len=strlen(pattern);
while(i<len)
{
while(j!=-1 && pattern[i]!=pattern[j]) j=next_[j];
next_[++i]=++j;
}
}
int KMP(char *text,char *pattern)
{
int i=0,j=0;
int len1=strlen(text),len2=strlen(pattern);
getNext(pattern);
while(i<len1 && j<len2)
{
if(j==-1 || pattern[j]==text[i])
j++,i++;
else
j=next_[j];
}
if(i>=len1) return j;
return 0;
}
int main(void)
{
// freopen("c.txt","r",stdin);
while(~scanf("%s%s",str1,str2))
{
int x=KMP(str1,str2);
int y=KMP(str2,str1);
if(x==y)
{
if(strcmp(str1,str2)>0)
{
printf("%s",str2);
printf("%s\n",str1+x);
}
else
{
printf("%s",str1);
printf("%s\n",str2+x);
}
}
else if(x>y)
{
printf("%s",str1);
printf("%s\n",str2+x);
}
else
{
printf("%s",str2);
printf("%s\n",str1+y);
}
}
return 0;
}
- KMP算法(2)-KMP算法的基础
- KMP算法2
- KMP算法小结 2
- KMP算法(2)
- KMP算法(2)
- KMP算法2
- KMP算法2
- 字符串查找(2)KMP算法
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- 每日算法2:KMP字符串匹配算法
- 算法之 KMP匹配算法2
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- Windows下怎么配置python的matplotlib环境
- Xilinx SDK 初学之--PS对PL寄存器的读写控制
- 排序算法总结
- 纯css实现进度条效果
- Maven+myeclipse 创建聚合项目
- KMP算法2
- Caffe学习系列(6):Blob,Layer and Net以及对应配置文件的编写
- 系统结构中的中断
- 设计模式--六大设计原则
- 泊松分布 二项分布 正态分布之间的联系
- android复习选择填空
- 三个最短路算法Dijkstra's;floyd;SPFA
- L3-010. 是否完全二叉搜索树 (对完全二叉树/层次遍历的理解)
- springMvc ajax请求 报406错误