c语言sunday算法代码
时间: 2023-12-19 18:29:04 浏览: 34
以下是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;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)