赞
踩
在本期的字符串匹配算法中,我将给大家带来常见的两种经典的示例:
目录
大家看到上诉这段话时肯定是晦涩难懂的,需要例子支持
- int BF(char *str,char *sub) //str:主串 sub:子串
- {
- assert(str != NULL && sub != NULL);
- if(str == NULL || sub == NULL)
- {
- return -1;
- }
-
- int i = 0;
- int j = 0;
- int strLen = strlen(str);
- int subLen = strlen(sub);
-
- while(i < strLen && j < subLen)
- {
- if(str[i] == sub[j])
- {
- i++;
- j++;
- }
- else
- {
- //回退
- i = i-j+1;
- j = 0;
- }
- }
-
- if(j >= subLen)
- {
- return i-j;
- }
-
- return -1;
- }
- int main()
- {
- printf("%d\n",BF("ababcabcdabcde","abcd"));
- printf("%d\n",BF("ababcabcdabcde","abcde"));
- printf("%d\n",BF("ababcabcdabcde","abcdef"));
- return 0;
- }
j 的回退位置
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。