POJ 3461和COJ 1248 KMP算法
2013-10-11 17:31
260 查看
COJ 1248
【算法实验项目】串匹配问题
Time Limit: 1000 ms Memory Limit: 65536 KBTotal Submit: 31 Accepted: 5
Description
给定一个文本,在该文本中查找并定位任意给定字符串。
Input
有两行。
第一行为主串S,(长度大于1,不大于1000000)
第二行为子串T,(长度大于1,不大于10000)
Output
输出只有一行。
如果子串在母串中出现,输出所有能和子串匹配的母串的起始位置。并以空格隔开(位置从0开始)
如果子串不存在于母串中输出-1.
Sample Input
abcbcbcabcb
bcb
Sample Output
1 3 8
Hint
从母串的第1、3、8位开始,都可以和子串匹配
推荐KMP算法
思路:KMP算法……慢慢来,把KMP吃透了……
#include <iostream> #include <map> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; char str[1000005],str1[10005]; int next[10005],len,len1,p=1; void getnext() { int k=-1,j=0; next[0]=-1; while(j<len1) { if(k==-1||str1[j]==str1[k]) { j++; k++; if(str1[j]!=str1[k]) next[j]=k; else next[j]=next[k]; } else k=next[k]; } } void kmp() { int i=0,j=0,k=0; while(i<len) { if(str1[j]==str[i]) { i++; j++; } else { j=next[j]; if(j==-1) { i++; j=0; } } if(j==len1) {printf("%d ",i-len1);p=0;} } } int main() { scanf("%s%s",str,str1); len=strlen(str); len1=strlen(str1); getnext(); kmp(); if(p) printf("-1\n"); else printf("\n"); return 0; }
POJ 3461
方法与上题相同……
#include <iostream> #include <map> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; char str[1000005],str1[10005]; int next[10005],len,len1,sum; void getnext() { int k=-1,j=0; next[0]=-1; while(j<len1) { if(k==-1||str1[j]==str1[k]) { j++; k++; if(str1[j]!=str1[k]) next[j]=k; else next[j]=next[k]; } else k=next[k]; } } void kmp() { int i=0,j=0,k=0; while(i<len) { if(str1[j]==str[i]) { i++; j++; } else { j=next[j]; if(j==-1) { i++; j=0; } } if(j==len1) sum++; } } int main() { int t; cin>>t; while(t--) { sum=0; scanf("%s%s",str1,str); len=strlen(str); len1=strlen(str1); getnext(); kmp(); cout<<sum<<endl; } return 0; }
相关文章推荐
- 【分析电脑快速关机是好还是坏】
- ZT 怎么样才算熟悉设计模式? [问题点数:40分,结帖人jiaoyun007]
- iSCSI
- 快速排序
- ARM裸机程序开发18(S3C2440A.s)
- std::set、自定义类型与比较函数
- Certificates, Identifiers & Profiles创建总结
- Lucene之MinShouldMatchScorer算法源码分析
- 字符串完美度
- C#学习笔记
- CocoaPods 的安装与使用
- FastDfs点滴
- 产品经理岗位说明书
- 理解 javascript 作用域
- Codeforces Round #119 (Div. 2) A题
- Python 解析XML
- 基于Hyper-V3.0搭建XenDesktop7之十一 部署Win8虚拟桌面之模板准备
- 获取SharePoint 2010文档库的文件夹及子文件夹
- Quartz表达式生成器
- c#遍历文件夹下所有文件并更改文件名