c语言sunday算法代码
时间: 2023-12-19 14:29:04 浏览: 182
以下是C语言实现的Sunday算法代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
int sunday(char* str, char* pattern) {
int len_str = strlen(str);
int len_pattern = strlen(pattern);
int jump[256];
for (int i = 0; i < 256; i++) {
jump[i] = len_pattern + 1;
}
for (int i = 0; i < len_pattern; i++) {
jump[pattern[i]] = len_pattern - i;
}
int i = 0;
while (i <= len_str - len_pattern) {
int j = 0;
while (j < len_pattern && str[i + j] == pattern[j]) {
j++;
}
if (j == len_pattern) {
return i;
}
i += jump[str[i + len_pattern]];
}
return -1;
}
int main() {
char str[MAX_LEN];
char pattern[MAX_LEN];
printf("请输入字符串:");
scanf("%s", str);
printf("请输入模式串:");
scanf("%s", pattern);
int pos = sunday(str, pattern);
if (pos == -1) {
printf("未找到匹配的子串\n");
} else {
printf("匹配的子串位置为:%d\n", pos);
}
return 0;
}
```
阅读全文