-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.c
More file actions
111 lines (99 loc) · 2.58 KB
/
String.c
File metadata and controls
111 lines (99 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdlib.h>
#include <stdio.h>
#include "define.h"
#include <string.h>
/*
|-------------------------------------------------------------------------------------------------
|字符串匹配,包括朴素算法和KMP算法;结果是以字符串的第一个
|字符的位置为1(不是0)来展示的;str是“主串”,match是要匹配
|的;
|-------------------------------------------------------------------------------------------------
*/
/**
* 初始化字符串
*/
int static initial(PString str, PString match)
{
//输入字符串和要匹配的字符串
printf("Input the original string\n");
gets(str->str);
printf("Input the matched string\n");
gets(match->str);
//计算长度
str->length = strlen(str->str);
match->length = strlen(match->str);
if(match->length > str->length){
printf("The matched string length is bigger than the string");
exit(-1);
}
}
/**
* 朴素算法
*/
int simplicity(PString str, PString match)
{
int i = 0, j = 0;
while(i < str->length && j < match->length){
//如果主串中要匹配的字符串长度小于要匹配的字符串长度就停止
if((str->length - i < match->length) && j == 0) break;
if(str->str[i] == match->str[j]) {
j++;
i++;
}
else{
//开始的时候i和j是相等的,只要匹配不成功i就比j多1
i = i - j + 1;
j = 0;
}
}
//printf("%d\n", i);
if(j >= match->length) return i - j + 1;
else return 0;
}
/**
* KMP算法获取匹配串的next数组
*/
int getNext(PString match, int *next)
{
int j = 0, i = 1;
next[0] = next[1] = 0;
while(i < match->length){
if(match->str[i] == match->str[j] || j == 0){
i++;
j++;
if(match->str[i] != match->str[j]) next[i] = j;
else next[i] = next[j];
}
else j = next[j];
}
}
/**
* KMP算法
*/
int KMP(PString str, PString match)
{
int i = 1, j = 1;
int next[match->length];
//获取匹配串的next数组
getNext(match, next);
while(i <= str->length && j <= match->length){
if(str->str[i - 1] == match->str[j - 1] || j == 0){
i++;
j++;
}
else j = next[j];
}
if(j > match->length) return i - j +1;
else return 0;
}
int testString()
{
int position;
String str, match;
initial(&str, &match);
//朴素算法
//position = simplicity(&str, &match);
//kmp算法
position = KMP(&str, &match);
printf("%d\n", position);
}