sdut oj2125 数据结构实验之串二:字符串匹配(BF与KMP做法)
2016-08-04 09:23
501 查看
题目链接:点击打开链接
需要注意的是动态字符数组要自行置串结束的标志‘\0’;
代码实现:
法二:
KMP做法:
需要注意的是动态字符数组要自行置串结束的标志‘\0’;
数据结构实验之串二:字符串匹配
Time Limit: 1000MS Memory limit: 65536K
题目描述
给定两个字符串string1和string2,判断string2是否为string1的子串。输入
输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2,string1和string2中保证不出现空格。(string1和string2大小不超过100字符)输出
对于每组输入数据,若string2是string1的子串,则输出"YES",否则输出"NO"。示例输入
abc a 123456 45 abc ddd
示例输出
YES YES NO
提示
BF算法代码实现:
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define maxsize 300 typedef struct { char *ch; int length; int stringsize; }Sqstring; int initString(Sqstring &S) { S.ch = (char *)malloc(maxsize *sizeof(char)); ///S.ch = new char[maxsize]; if(!S.ch) return -1; S.length = 0; S.stringsize = maxsize; return 0; } void Creat(Sqstring &S, char str[]) { int i; int len = strlen(str); for(i = 0;i < len;i++) { S.ch[i] = str[i]; S.length++; } S.ch[i] = '\0';///置串S的结束标志!! } void Index(Sqstring &S1,Sqstring &S2) { int i = 0,j = 0; while(S1.ch[i + j] != '\0'&&S2.ch[j] != '\0') { if(S1.ch[i + j] == S2.ch[j]) j++; else { i++; j=0; } } if(S2.ch[j] == '\0') printf("YES\n"); else printf("NO\n"); } int main() { char str1[110],str2[110]; Sqstring S1,S2; while(~scanf("%s%s",str1,str2)) { initString(S1); initString(S2); Creat(S1,str1); Creat(S2,str2); Index(S1,S2); } return 0; }
法二:
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define maxsize 1010 typedef struct { char *ch; int length; int stringsize; }Sqstring; int initString(Sqstring &S) { S.ch = (char *)malloc(maxsize*sizeof(char )); if(!S.ch) return -1; S.length = 0; S.stringsize = maxsize; return 0; } void Creat(Sqstring &S,char s[]) { int i; int len = strlen(s); for(i = 0;i < len;i++) { S.ch[i] = s[i]; S.length ++; } S.ch[i] = '\0'; } void Index(Sqstring &S1,Sqstring &S2) { int i = 0,j = 0; while(i < S1.length&&j < S2.length) { if(S1.ch[i] == S2.ch[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if(j >= S2.length) printf("YES\n"); else printf("NO\n"); } int main() { char a[200],b[200]; while(~scanf("%s%s",a,b)) { Sqstring S1,S2; initString(S1); initString(S2); Creat(S1,a); Creat(S2,b); Index(S1,S2); } return 0; }
KMP做法:
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define maxsize 1010 int next[100100]; typedef struct { char *ch; int length; int stringsize; }Sqstring; int initString(Sqstring &S) { S.ch = (char *)malloc(maxsize*sizeof(char )); if(!S.ch) return -1; S.length = 0; S.stringsize = maxsize; return 0; } void Creat(Sqstring &S,char s[]) { int i; int len = strlen(s); for(i = 0;i < len;i++) { S.ch[i] = s[i]; S.length ++; } S.ch[i] = '\0'; } void Get_next(Sqstring &S) { int i = 0,j = -1; next[0] = -1; while(i < S.length) { if(j == -1||S.ch[i] == S.ch[j]) { i++; j++; next[i] = j; } else j = next[j]; } } void Index(Sqstring &S1,Sqstring &S2) { Get_next(S2); int i = 0,j = 0; while(i < S1.length&&j < S2.length) { if(j == -1||S1.ch[i] == S2.ch[j]) { i++; j++; } else { j = next[j]; } } if(j >= S2.length) printf("YES\n"); else printf("NO\n"); } int main() { char a[200],b[200]; while(~scanf("%s%s",a,b)) { Sqstring S1,S2; initString(S1); initString(S2); Creat(S1,a); Creat(S2,b); Index(S1,S2); } return 0; }
相关文章推荐
- 字符串匹配从BF到BM再到KMP(一个非主流NEXT函数)
- BF,KMP,BM三种字符串匹配算法性能比较
- 字符串匹配算法——BF、KMP、Sunday
- 字符串匹配的KMP做法
- 字符串匹配常见算法(BF,RK,KMP,BM,Sunday)
- SDUT OJ 数据结构实验之串二:字符串匹配(KMP做法)
- 字符串匹配算法(BF KMP)
- 字符串匹配算法之BF vs KMP
- BF_KMP字符串匹配
- 字符串匹配(BF,KMP,BM)
- 字符串匹配算法BF和KMP总结
- BF到KMP,再到后缀数组的字符串匹配
- 【KMP】SDUTOJ2125 串结构练习——字符串匹配
- {模版}KMP字符串匹配
- 字符串匹配-KMP
- [数据结构]字符串匹配(KMP方法)
- C++版字符串匹配算法之传统匹配算法加KMP字符串匹配算法
- 模板 kmp字符串匹配
- 洛谷P3375【模板】KMP字符串匹配
- 字符串匹配---KMP