KMP 练习
2015-08-13 14:48
330 查看
A - KMP模式匹配 一(串)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Submit Status
Description
求子串的next值,用next数组存放,全部输出
Input
输入一个字符串
Output
输出所有next值
Sample Input
Sample Output
0 1 1 2 2 3 1 2
B - KMP模式匹配 二(串)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status
Description
输入一个主串和一个子串,用KMP进行匹配,问进行几趟匹配才成功,若没成功,则输出0
Input
输入一个主串和一个子串
Output
匹配的趟数
Sample Input
Sample Output
3
C - KMP模式匹配 三(串)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status
Description
输入一个主串和一个子串,若匹配成功,则找出匹配的趟数和在子串在主串中的位置,若匹配不成功,则输出0
Input
输入两个字符串
Output
输出匹配的趟数和位置
Sample Input
Sample Output
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Submit Status
Description
求子串的next值,用next数组存放,全部输出
Input
输入一个字符串
Output
输出所有next值
Sample Input
abaabcac
Sample Output
0 1 1 2 2 3 1 2
#include <iostream> #include <cstring> using namespace std; int main(){ char s[100]; cin>>s; int suffix[100]; int len=strlen(s); int pos=0; suffix[0]=-1; suffix[1]=0; for(int i=2;i<=len;++i){ while(pos>=0&&s[pos]!=s[i-1]) pos=suffix[pos]; suffix[i]=++pos; } int i=1; cout<<suffix[0]+1; while(i<strlen(s)){ cout<<' '<<suffix[i]+1; i++; } return 0; }
B - KMP模式匹配 二(串)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status
Description
输入一个主串和一个子串,用KMP进行匹配,问进行几趟匹配才成功,若没成功,则输出0
Input
输入一个主串和一个子串
Output
匹配的趟数
Sample Input
ababcabcacbab abcac
Sample Output
3
#include <iostream> #include <cstring> using namespace std; int main(){ char s[100]; char b[100]; cin>>b; cin>>s; int suffix[100]; int len=strlen(s); int pos=0; suffix[0]=-1; suffix[1]=0; for(int i=2;i<=len;++i){ while(pos>=0&&s[pos]!=s[i-1]) pos=suffix[pos]; suffix[i]=++pos; } int i=0,j=0; int cns=1; bool flag=false; while(i<strlen(b)) if(b[i]==s[j]){ j++; i++; if(s[j]=='\0'){ flag=true; break; } } else if(suffix[j]==-1){ i++; j=0; cns++; } else { j=suffix[j]; cns++; } if(flag)cout<<cns<<'\12'; else cout<<0<<'\12'; return 0; }
C - KMP模式匹配 三(串)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status
Description
输入一个主串和一个子串,若匹配成功,则找出匹配的趟数和在子串在主串中的位置,若匹配不成功,则输出0
Input
输入两个字符串
Output
输出匹配的趟数和位置
Sample Input
ababcabcacbab abcac
Sample Output
3 6
#include <iostream> #include <cstring> using namespace std; int main(){ char s[100]; char b[100]; cin>>b; cin>>s; int suffix[100]; int len=strlen(s); int pos=0; suffix[0]=-1; suffix[1]=0; for(int i=2;i<=len;++i){ while(pos>=0&&s[pos]!=s[i-1]) pos=suffix[pos]; suffix[i]=++pos; } int i=0,j=0; int cns=1; bool flag=false; while(i<strlen(b)) if(b[i]==s[j]){ j++; i++; if(s[j]=='\0'){ flag=true; break; } } else if(suffix[j]==-1){ i++; j=0; cns++; } else { j=suffix[j]; cns++; } //cout<<"i "<<i<<" j "<<j<<" len "<<strlen(b)<<'\12'; if(flag)cout<<cns<<' '<<i-j+1<<'\12'; else cout<<0<<'\12'; return 0; }
相关文章推荐
- Java - Difference between Stack and Heap memory in Java
- 计算机视觉-机器学习近年部分综述
- JS控制DIV隐藏显示
- Asp.net动态页面静态化之初始NVelocity模板引擎
- Download功能的实现
- 创建oracle数据库
- 简单的Queue
- Lua中的元表与元方法学习总结
- 在Visual Studio中使用用例图描述参与者与用例的关系
- MySQL的约束问题
- 一个“永不结束”的服务端进程
- 集成CCFlow工作流与GPM的办公系统驰骋CCOA介绍(三)
- EasyUI combobox下拉列表框--下拉列表框的onShowPanel事件
- 深入理解SELinux SEAndroid
- hdu 1823 Luck and Love
- Java post 请求
- HDU ACM 2010 水仙花数
- 浅谈 qmake 之 pro、pri、prf、prl文件
- 重新认识Segment fault
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)