kmp模式串2求匹配的趟数
2015-09-24 18:55
330 查看
Description
输入一个主串和一个子串,用KMP进行匹配,问进行几趟匹配才成功,若没成功,则输出0Input
输入一个主串和一个子串Output
匹配的趟数Sample Input
ababcabcacbab abcac
Sample Output
3
HINT
Source
#include <iostream> #include<string.h> #include<cstdio> using namespace std; int i,j,len1,len2; int next[101]; char s[101],t[101]; int main() { cin>>s; cin>>t; len1=strlen(s);//string类用a.length()函数 len2=strlen(t); int i=0,j=-1; next[0]=-1; while(i<len2)//求next值是专门针对子串的(t串) { if(j==-1||t[i]==t[j]) { i++; j++; //if(a[i]!=a[j]) // next[i]=j; // else next[i]=j; } else j=next[j]; }//已经算出了next数组 i=0,j=0; int cnt=1; bool flag=false; while(i<len1)//遍历所有的s串 { if(s[i]==t[j]) { i++; j++; if(t[j]=='\0')//t串到头,flag=1标识 { flag=true; break; } } else if(next[j]==-1) { j=0; i++; cnt++; } else//j==-1||t[i]==t[j]排除后 { j=next[j]; cnt++; } } if(flag) cout<<cnt<<endl; else cout<<"0"<<endl; return 0; }
相关文章推荐
- Git 多人协同开发
- oracle db于,一个特定的数据字典pct miss其计算公式
- jquery each 用法
- Java 动态代理机制分析及扩展,第 1 部分
- 单例模式/singleton模式/创建型模式
- Eclipse 中使用Genymotion 作为模拟器的步骤
- UI_UIScrollView(滚动视图)的图文理解
- C#读取Modbus数据
- Python 反转字典 即key和val互换
- MySQL语句内求和 选择语句外的表进行查询求和
- javaEE 使用过滤器压缩响应内容
- POJ 1270 Follwing Orders
- 数据结构 —— 第一个程序 【顺序表】
- 关于MIUI悬浮窗权限问题的解决方案的一点补充
- 计算窗口边框厚度、标题栏高度最正确的方法
- C#中WinForm窗体事件的执行次序如下:
- 协议Protocol
- window开机启动项设置和取消方法
- 十月金在线编程竞赛的第二个冠军:解密
- pip的一些问题