一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C/C++ - C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題

C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題

2021-06-08 15:30llcomeon1_ C/C++

這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下

C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題

 主要實(shí)現(xiàn)了三種字符串的模式匹配,主要包括字符串子操作的集合,字符串指針回溯,和KMP算法 

頭文件 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef INDEXHEAD_H_INCLUDED
#define INDEXHEAD_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 255
typedef char Sstring[MAXLEN + 1] ;
int StrAssign( Sstring str , char* ps ) ;
int StrLength( Sstring str ) ;
int StrPrint( Sstring str ) ;
int StrCompare( Sstring str1 , Sstring str2 ) ;
int StrSub( Sstring sub , Sstring str , int pos , int length ) ;
int StrIndex1( Sstring str , Sstring sub , int pos ) ;
int StrIndex2( Sstring str , Sstring sub , int pos ) ;
int StrIndex3( Sstring str , Sstring sub , int pos ) ;
int GetNext( Sstring str , int next[] ) ;
#endif // INDEXHEAD_H_INCLUDED

函數(shù)實(shí)現(xiàn)

?
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "indexhead.h"
int StrAssign( Sstring str , char* ps )
{
  int i = 0 ;
  if( strlen( ps ) > MAXLEN )
  {
    printf( "ERROR!\n" ) ;
    exit( 1 ) ;
  }
  str[i++] = strlen( ps ) ;
  while( i <= strlen( ps ) )
  {
    str[i] = *( ps + i - 1 ) ;
    i++ ;
  }
  return 0 ;
}
int StrPrint( Sstring str )
{
  int i = 1 ;
  while( i <= str[0] )
  {
    printf( "%c" , str[i++] ) ;
  }
  printf( "\n" ) ;
  return 0 ;
}
int StrLength( Sstring str )
{
  return str[0] ;
}
int StrCompare( Sstring str1 , Sstring str2 )
{
  int i = 1 ;
  int ret = 0 ;
  while( i <= str1[0] && i <= str2[0] )
  {
    ret = ( unsigned char )str1[i] - ( unsigned char )str2[i] ;
    if( ret < 0 )
    {
      return -1 ;
    }
    else if( ret > 0 )
    {
      return 1 ;
    }
    else
    {
      i++ ;
    }
  }
  if( i <= str1[0] )
  {
    return -1 ;
  }
  else if( i <= str2[0] )
  {
    return 1 ;
  }
  else
  {
    return 0 ;
  }
}
int StrSub( Sstring sub , Sstring str , int pos , int length )
{
  if( pos < 1 || pos > str[0] || length < 0 || length > str[0] - pos + 1 )
  {
    printf( "ERROR!\n" ) ;
    exit( 1 ) ;
  }
  int i = 1 ;
  sub[0] = length ;
  while( i <= length )
  {
    sub[i] = str[pos + i - 1] ;
    i++ ;
  }
  return 0 ;
}
int StrIndex1( Sstring str , Sstring sub , int pos )
{
  pos = 1 ;
  Sstring stemp ;
  while( pos <= str[0] - sub[0] + 1 )
  {
    StrSub( stemp , str , pos , sub[0] ) ;
    if( !StrCompare( stemp , sub ) )
    {
      return pos ;
    }
    pos++ ;
  }
  return 0 ;
}
int StrIndex2( Sstring str , Sstring sub , int pos )
{
  if( pos < 1 || pos > str[0] - sub[0] + 1 )
  {
    printf( "ERROR!\n" ) ;
    exit( 1 ) ;
  }
  int i = pos ;
  int j = 1 ;
  while( ( i <= str[0] - sub[0] + 1 ) && ( j <= sub[0] ) )
  {
    if( str[i] == sub[j] )
    {
      i++ ;
      j++ ;
    }
    else
    {
      i = i - j + 2 ;
      j = 1 ;
    }
  }
  if( j > sub[0] )
  {
    return i - sub[0] ;
  }
  return 0 ;
}
int GetNext( Sstring str , int next[] )
{
  int i = 1 ;
  next[1] = 0 ;
  int j = 0 ;
  while( i < str[0] )
  {
    if( j== 0 || str[i] == str[j] )
    {
      ++i ;
      ++j ;
      next[i] = j ;
    }
    else
    {
      j = next[j] ;
    }
  }
  return 0 ;
}
int StrIndex3( Sstring str , Sstring sub , int pos )
{
  int i = pos ;
  int j = 1 ;
  int next[ sub[0] ] ;
  GetNext( sub , next ) ;
  while( i <= str[0] && j <= sub[0] )
  {
    if( j == 0 || str[i] == sub[j] )
    {
      ++i ;
      ++j ;
    }
    else
    {
      j = next[j] ;
    }
  }
  if( j > sub[0] )
  {
    return i - sub[0] ;
  }
  return 0 ;
}

 測(cè)試匹配函數(shù)

?
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
#include "indexhead.h"
int main()
{
  /*Sstring str ;
  Sstring str1 ;
  char* p = "hello" ;
  StrAssign( str , p ) ;
  StrAssign( str1 , "ahello" ) ;
  StrPrint( str ) ;
  int i = StrLength( str ) ;
  printf( "%d\n" , i ) ;
  int j = StrCompare( str , str1 ) ;
  printf( "%d\n" , j ) ;
  StrSub( str , str1 , 3 , 4 ) ;
  StrPrint( str ) ;*/
  /*Sstring str1 ;//驗(yàn)證StrIndex1()
  Sstring sub ;
  StrAssign( str1 , "shfiodshfdghafhs" ) ;
  StrAssign( sub , "dgh" ) ;
  int i = StrIndex1( str1 , sub , 1 ) ;
  printf( "%d\n" , i ) ;*/
  /*Sstring str1 ;//驗(yàn)證StrIndex2()
  Sstring sub ;
  StrAssign( str1 , "shfiodshfdghafhs" ) ;
  StrAssign( sub , "dgh" ) ;
  int i = StrIndex2( str1 , sub , 1 ) ;
  printf( "%d\n" , i ) ;*/
  Sstring str1 ;//驗(yàn)證StrIndex3()
  Sstring sub ;
  StrAssign( str1 , "shfiodshfdghafhs" ) ;
  StrAssign( sub , "dgh" ) ;
  int i = StrIndex3( str1 , sub , 1 ) ;
  printf( "%d\n" , i ) ;
  return 0;
}

如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

原文鏈接:http://blog.csdn.net/llcomeon1_/article/details/7771487

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本高清视频一区二区 | 91传媒在线观看 | 成人资源影音先锋久久资源网 | 99视频免费在线 | youwu在线影院| 欧美日韩高清不卡一区二区三区 | 美女脱了内裤打开腿让人桶网站o | 国产精品成人自拍 | 久久久久国产一级毛片高清片 | 调教女警花穿环上班 | 国产精品麻豆99久久 | 爱草影院 | 3d动漫免费 | 国产日韩一区二区三区 | 国产精品一区二区在线观看完整版 | 精品AV无码一二三区视频 | 99福利在线观看 | 无套内射在线观看THEPORN | 香蕉国产人午夜视频在线观看 | 日本综合在线观看 | www.国产在线观看 | 色先锋影音先锋 | 女女性恋爱免费 | 奇米影视先锋 | 私人黄色 | 91传媒制片厂制作传媒破解版 | 欧美成人免费tv在线播放 | 日本三级免费网站 | 四虎最新永久免费视频 | 午夜第九达达兔鲁鲁 | 国产一级免费片 | yellow视频在线观看 | 日韩成人免费aa在线看 | 久久午夜一区二区 | 精品国产线拍大陆久久尤物 | 亚洲六月丁香婷婷综合 | 国产欧美一区二区三区免费看 | 91四虎国自产在线播放线 | 亚洲小视频网站 | 亚洲天堂色视频 | 婷婷99视频精品全部在线观看 |