对于程序的语法报错问题,就是根据提示去找问题,不要自己瞎猜(猜的基本都是错的)
1.其中对于C语言的函数申明:extern int strStr(char * x, char * y)
如果不根据提示查,根本就解决不了。
2.关于数据类型的不匹配问题也是需要根据提示来改正。
3.溢出需要查看循环语句是不是超过了数组的边界。
解决了语法编译的问题,但是还是不能输出结果:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern int strStr(char * x, char * y);
extern int indexKMP(char *x, char *y,int z);
int main(){
char * haystack = "sssdjiefon";
char * needle = "st";
int result = strStr(haystack,needle);
return result;
}
int strStr(char * haystack, char * needle){
int j = indexKMP (haystack, needle, 1);
return j;
}/**
数据结构中的伪代码是不符合语法的,一个char数组却在首部存了整个数组的长度(int类型)
*/
int * getnext(char* T,int *next){
int length = strlen(T);
next = (int *) malloc(sizeof(int)*length + 1);
int i = 0;next[1] = 0; int j = 0; //next[i]表示的是有待匹配的字符在字符串中的位置
while(i < length){ //设计上模式串是从下标为0的位置开始匹配的
if(j == 0 || T[i] == T[j]){ //next[]的设计需要和KMP具体的匹配函数相匹配 (如果模式串的第一个字符就和主串匹配,主串和模式串都需要向右边转移)
++i;++j;next[i] = j; //因为在设计上是模式串是从1开始的
}
else{
j = next[j]; //告知模式串需要右移动的位数
}
}
return next;
}
/**
KMP算法的思路
*/
int indexKMP(char *S, char *T,int pos){
//需要利用T的next数组进行 利用模式串T的next函数求主串S中第pos个字符之后的位置
int lengths = strlen(S);
int lengtht = strlen(T);
int *next =(int *) malloc(sizeof(int)*lengtht +1);
next = getnext(S,next);
int i = pos; int j = 1;
while(i <= lengths && j <= lengtht ){
if(j == 0 && S[i] == T[j]){
++i;++j;
}
else{
j = next[j];
}
if(j > T[0]) return i-T[0];
else return -1;
}
}
C语言数组的大小szieof是问题的。。。。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern int strStr(char *x, char *y);
extern int indexKMP(char *x, char *y, int z);
int main()
{
char *haystack = "abaabcac";
char *needle = "abaabcac";
printf("%d 你太蔡了 \n", *haystack);
int result = strStr(haystack, needle);
return result;
}
int strStr(char *haystack, char *needle)
{
int j = indexKMP(haystack, needle, 0);
printf("\n");
printf("%d 鸡你太美 \n", j);
return j;
} /**
数据结构中的伪代码是不符合语法的,一个char数组却在首部存了整个数组的长度(int类型)
*/
int *getnext(char *T)
{
int length = strlen(T);
printf(" ------------%d",length);
int* next = (int*)malloc((length + 1) * sizeof(int));
int sss = sizeof(next)/sizeof(int);
printf(" ------------sss---%d\n",sss);
int i = 0;
next[0] = -1;
int j = -1; //next[i]表示的是有待匹配的字符在字符串中的位置
for (int k = 0; k < length; k++)
{
printf("%c \n", T[k]);
}
while (i < length - 1)
{ //设计上模式串是从下标为0的位置开始匹配的
if (j == -1 || T[i] == T[j])
{ //next[]的设计需要和KMP具体的匹配函数相匹配 (如果模式串的第一个字符就和主串匹配,主串和模式串都需要向右边转移)
++i;
++j;
next[i] = j; //因为在设计上是模式串是从1开始的
printf(" next[%d] = %d \n", i, next[i]);
int lengthm1 = sizeof(next) / sizeof(int);
printf("太美666688888===== %d \n ", lengthm1);
}
else
{
j = next[j]; //告知模式串需要右移动的位数
}
}
printf("\n");
int m = 0;
int lengthm = 8;
printf("太美6666===== %d \n ", lengthm);
for (; m < 8; m++)
{
printf("太美next = %d \n", next[m]);
}
// printf("%d 你实在是太美 \n", next[1]);
return next;
}
/**
KMP算法的思路
*/
int indexKMP(char *S, char *T, int pos)
{
//需要利用T的next数组进行 利用模式串T的next函数求主串S中第pos个字符之后的位置
int lengths = strlen(S);
int lengtht = strlen(T);
printf("t=====%d \n", lengtht);
// printf("%d", lengtht);
// int *next = (int *)malloc(sizeof(int) * lengtht + 1);
int *next = getnext(T);
int lengthm = sizeof(next) / sizeof(next[0]);
printf("lengthm111=====%d", lengthm);
for (int m = 0; m < lengthm; m++)
{
printf(" 你实在是太美===== %d \n", next[m]);
}
int i = pos;
int j = 0;
while (i < lengths && j < lengtht)
{
printf("是不是很美?");
if (j == 0 || S[i] == T[j])
{
++i;
++j;
printf("%d 确实很美 \n", i);
}
else
{
j = next[j];
printf("jjj ===== %d \n", j);
}
if (j == lengths - 1)
{
printf("i ==== %d \n", i);
printf("lengths ==== %d \n", lengths);
return i - lengths;
}
else
return -1;
}
}