串的匹配算法

  1 #include<string.h>
  2 #include<string> 
  3 #include<iostream>
  4 using namespace std;
  5 #define OK 1
  6 #define ERROR 0
  7 #define OVERFLOW -2
  8 typedef int Status;
  9 #define MAXSIZE 100
 10 //定义串结构体       
 11 typedef struct{
 12     char *ch;
 13     int length;
 14 }SString; 
 15 
 16 Status initStr(SString &S)
 17 {
 18     S.ch= new char[MAXSIZE];
 19     S.length=0;
 20 }
 21 Status StrAssign(SString &T, string chars) { //生成一个其值等于chars的串T
 22     int i;
 23     if (chars.length() > MAXSIZE)
 24         return ERROR;
 25     else {
 26         T.length = chars.length();
 27         for (i = 1; i <= T.length; i++)
 28             T.ch[i] = chars[i-1];
 29         return OK;
 30     }
 31 }
 32 
 33 //算法4.1 BF算法
 34 int Index(SString S, SString T, int pos)
 35 {
 36     //返回模式T在主串S中第pos个字符之后第s一次出现的位置。若不存在,则返回值为0
 37     //其中,T非空,1≤pos≤StrLength(S)
 38     int i = pos;
 39     int j = 1;
 40     while(i <= S.length&&j <= T.length)
 41     {
 42         if(S.ch[i]==T.ch[j])
 43         {
 44             ++i;
 45             ++j;
 46         } //继续比较后继字符
 47         else
 48         {
 49             i=i-j+2;
 50             j=1;
 51         } //指针后退重新开始匹配
 52     }
 53     if (j > T.length)
 54         return i - T.length;
 55     else
 56         return 0;
 57     return 0;
 58 }//Index
 59 
 60 int main()
 61 {
 62     SString S,T;
 63     initStr(S);
 64     initStr(T);
 65     string a="bbaaabbaba";
 66     string b="abb";
 67     StrAssign(S,a) ;
 68     StrAssign(T,b) ;
 69     cout<<"主串和子串在第"<<Index(S,T,1)<<"个字符处首次匹配\n";
 70     return 0;
 71 }
 72 #include<string.h>
 73 #include<string> 
 74 #include<iostream>
 75 using namespace std;
 76 #define OK 1
 77 #define ERROR 0
 78 #define OVERFLOW -2
 79 typedef int Status;
 80 #define MAXSIZE 100
 81 //定义串结构体       
 82 typedef struct{
 83     char *ch;
 84     int length;
 85 }SString; 
 86 
 87 Status initStr(SString &S)
 88 {
 89     S.ch= new char[MAXSIZE];
 90     S.length=0;
 91 }
 92 Status StrAssign(SString &T, string chars) { //生成一个其值等于chars的串T
 93     int i;
 94     if (chars.length() > MAXSIZE)
 95         return ERROR;
 96     else {
 97         T.length = chars.length();
 98         for (i = 1; i <= T.length; i++)
 99             T.ch[i] = chars[i-1];
100         return OK;
101     }
102 }
103 
104 //算法4.1 BF算法
105 int Index(SString S, SString T, int pos)
106 {
107     //返回模式T在主串S中第pos个字符之后第s一次出现的位置。若不存在,则返回值为0
108     //其中,T非空,1≤pos≤StrLength(S)
109     int i = pos;
110     int j = 1;
111     while(i <= S.length&&j <= T.length)
112     {
113         if(S.ch[i]==T.ch[j])
114         {
115             ++i;
116             ++j;
117         } //继续比较后继字符
118         else
119         {
120             i=i-j+2;
121             j=1;
122         } //指针后退重新开始匹配
123     }
124     if (j > T.length)
125         return i - T.length;
126     else
127         return 0;
128     return 0;
129 }//Index
130 
131 int main()
132 {
133     SString S,T;
134     initStr(S);
135     initStr(T);
136     string a="bbaaabbaba";
137     string b="abb";
138     StrAssign(S,a) ;
139     StrAssign(T,b) ;
140     cout<<"主串和子串在第"<<Index(S,T,1)<<"个字符处首次匹配\n";
141     return 0;
142 }
143 #include<stdio.h>   
144 #define N 101 
145 char A[N],B[N];  
146 int BF(char a[],char b[])   
147 {                         
148  int i=0,j=0;     
149  while(a[i]&&b[j]){
150     if(a[i++]==b[j])
151         ++j;    
152     else{
153         i-=j; 
154         j=0; 
155         } 
156     }
157  return b[j]?-1:i-j+1;    
158 }  
159 int main(){ 
160     gets(A);
161     gets(B);
162     printf("%d\n",BF(A,B));
163    return 0;
164 }
165 #include<stdio.h>
166 #include<string.h>
167 char a[101],b[101];
168 int main(){
169     int m,n;
170     int x=1,y=1;
171     gets(a);
172     gets(b);
173     m=strlen(a);
174     n=strlen(b);
175     printf("%d  %d\n",m,n);
176     int i=1,j=1;
177     while(i<m&&j<n){
178         if(a[i]==b[j]){
179             i++;
180             j++;
181         }
182         else{
183             i=i-j+2;
184             j=1;
185         }
186     }
187     if(j>n)
188         printf("%d",i-j+2);
189     else
190         printf("-1");
191     return 0;   
192 } 

 

posted @ 2017-10-15 23:01  无心小男  阅读(354)  评论(0编辑  收藏  举报