KMP字符串匹配,字串
来源:互联网 发布:二分查找java 编辑:程序博客网 时间:2024/05/16 06:50
题意:输入多组,判断str2是否是str1的字串,是则输出串1在串2中的位置,否则输出-1;
- //当主串中第i个字符与模式中第j个字符“失配”时,
- //主串中第i个字符应该与模式中哪个字符再比较。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char str1[1000000],str2[1000000];
- int next[1000000];
- void getnext(int t)//next[j]表示当模式中第j个字符与主串中相应字符“失配”时,在模式中需要重新和主串中该字符进行比较的字符的位置。
- {
- int i,j;
- i=1;j=0;next[i]=0;
- while(i<=t)
- {
- if(j==0||str2[i-1]==str2[j-1])
- {
- i++;j++;
- next[i]=j;
- }
- else
- j=next[j];
- }
- }
- int KMP(int s,int t)
- {
- int i,j;
- i=1;j=1;
- while(i<=s&&j<=t)
- {
- if(j==0||str1[i-1]==str2[j-1])
- {
- i++;j++;
- }
- else
- j=next[j];
- }
- if(j>t)
- return (i-t);
- else
- return -1;
- }
- int main()
- {
- int s,t,g;
- while(~scanf("%s%s",str1,str2))
- {
- s=strlen(str1);
- t=strlen(str2);
- getnext(t);
- g=KMP(s,t);
- printf("%d\n",g);
- }
- return 0;
- }
0 0
- KMP字符串匹配,字串
- kmp匹配字串
- 数据结构 字串的模式匹配 KMP算法
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- 字符串匹配 KMP
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- KMP字符串匹配(1)
- KMP字符串匹配(2)
- KMP字符串匹配(3)
- KMP字符串匹配算法
- 黑马程序员—(iOS开发)iOS了解---(七)
- Contiki:RDC &CoAP
- Codeforces Round #263 (Div. 1)
- oracle 表连接与子查询示例
- 数据库Mysql存储,读取图片
- KMP字符串匹配,字串
- PAT 1009
- Android与服务器通讯:如何保证两端时间一致性
- Delphi 安装ICS
- HDU Bone Collector 2620 01背包
- CentOS平台实现Nagios+NagiosQL监控
- VBA 读取/写入文本文件
- UVa1151&POJ2784--Buy or Build【kruskal+二进制枚举】
- POJ 3498 March of the Penguins(枚举+最大流)