#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define INITSIZE 1000
#define INCRE 20
#define OK 1
#define ERROR 0
typedef struct {
char *data;
int length, stringsize;
} SqString;
//串初始化
int initString(SqString *S) {
S->data = (char *)malloc(sizeof(char) * INITSIZE);
if (!S->data)
return 0;
S->length = 0;
S->stringsize = INITSIZE;
return 1;
}
//串赋值
int strAssign(SqString *s, char *str ) {
int i = 0;
while (*str) {
s->data[i] = *str++;
i++;
}
s->data[i] = '\0';
s->length = i;
return 1;
}
//基本模式匹配算法
int index_bf(SqString *s, SqString *t, int start) {
int i = start - 1, j = 0;
while (j < t->length && i < s->length) {
if (s->data[i] == t->data[j]) {
j++;
i++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j >= t->length)
return i - t->length + 1;
else
return 0;
}
int main() {
//利用模式匹配算法完成子串查找
SqString s, t;
char str[INITSIZE];
char t1[INITSIZE];
if (initString(&s) && initString(&t)) {
scanf("%s", str);
strAssign(&s, &str[0]);
scanf("%s", t1);
strAssign(&t, &t1[0]);
int pos = 1;
int i = 0;
int count = 0;
while (pos <= s.length - t.length + 1 && pos != 0) {
i = index_bf(&s, &t, pos);
if (i == 0) {
break;
}
printf("%d ", i);
pos = ++i;
count = 1;
}
if (count == 0)
printf("0");
}
return 0;
}
BF
原创
©著作权归作者所有:来自51CTO博客作者wx641ed5d0bdd07的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
EF BB BF的问题
今天
EF BB BF UTF-8 另存为 字节流 编码方式 -
springboot请求转发到别的地址
作者 江南一点雨https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了。不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书。我印象中有效期一年,可以申请 20 个。今天要和大家聊的是在 Spring Boot 项目中,如何开启 https 配置,为我们的接口保驾护航。https 简介我们先来看看什么是 h
springboot请求转发到别的地址 springboot转发http请求 HTTP HTTPS java