kmp 公共子串出现3次
来源:互联网 发布:mac无法搜索移动硬盘 编辑:程序博客网 时间:2024/05/16 09:27
怎么那么巧妙。。自己没有想到。
转自http://www.cnblogs.com/19992147orz/p/6284076.html
#include<iostream>#include<map>#include<algorithm>#include<string>#include<cstring>#include<cstdio>#include<iomanip>using namespace std;const int maxn=1000005;char s[maxn];int nxt[maxn];int used[maxn];void getnext(){ int len=strlen(s); int k=-1; int j=0; nxt[0]=-1; while(j<=len-1){ if(k==-1||s[j]==s[k]){ j++; k++; nxt[j]=k; } else{ k=nxt[k]; } }}void init(){ memset(used,0,sizeof(used)); memset(nxt,0,sizeof(nxt));}int main(){ while(cin>>s){ init(); getnext(); int len=strlen(s); for(int i=1;i<=len-1;i++)used[nxt[i]]=1;//用这方法就巧妙地处理了中间有公共子串的情况 used[0]=0; for(int i=len;i!=-1;i=nxt[i]) if(used[nxt[i]]){ for(int j=0;j<nxt[i];++j)cout<<s[j]; cout<<endl; return 0; } printf("Just a legend\n"); }}
0 0
- kmp 公共子串出现3次
- poj1226 kmp 最长公共子串
- Codevs_P1425 最长公共子串(KMP)
- KMP算法——公共子串
- HDU1238 Substrings(kmp,最长公共子串)
- Poj 3294 Life Forms (后缀数组 在n个串中出现k次的最长公共子串并输出)
- kmp统计子串出现次数
- KMP模版:子串是否出现
- 【KMP多串最长公共子串】POJ 3080
- 【KMP求多个串的最长公共子串】POJ 3450
- POJ 3080 查找公共最长子串 kmp+枚举
- poj 3450 Corporate Identity (KMP+最长公共子串)
- poj 3080 Blue Jeans (KMP+最长公共子串)
- poj3080(Blue Jeans)kmp求多个串公共子串
- POJ 3080 Blue Jeans(KMP 最长公共子串)
- POJ 3080 Blue Jeans(KMP 最长公共子串)
- poj--3450 KMP求多个字符串的最长公共子串
- poj 3450 Corporate Identity 【KMP】【求公共子串】
- Vulkan Programming Guide 第一章 第2节
- apue 获取用户信息
- 类似qq,微信的消息提醒
- JS实现随机验证码功能
- 算法18:实现memmove函数
- kmp 公共子串出现3次
- Java 中的 Reference
- C# 如何实现单侧
- 关于 nginx 的配置的 location
- 深度学习Matlab工具箱代码详解
- 3D激光扫描三维重建——1.matlab相机标定
- 近200篇机器学习&深度学习资料分享(小象学院)
- Josephu问题
- 编译&链接