字符串匹配

这里介绍两种常见算法, Brute Force和KMP. 

代码如下:

  1 //字符串匹配
  2 //Brute Force
  3 class StringPattern {
  4 private:
  5     string value;
  6 public:
  7     StringPattern(const string &input) {
  8         value = input;
  9     }
 10     int match(const string &buffer){
 11         //注意当buffer.length()<value.length()时, buffer.length()-value.length()的值是一个大整数
 12         //所以需要用if判断
 13         if(buffer.length()<value.length()){
 14             return -1;
 15         }
 16         for(int i=0;i<=(buffer.length()-value.length());i++){
 17             int j=0;
 18             for(;j<value.length();j++){
 19                 if(buffer[i+j]!=value[j]){
 20                     break;
 21                 }
 22             }
 23             if(j==value.length()){
 24                 return i;
 25             }
 26         }
 27         return -1;
 28     }
 29 };
 30 
 31 int main() {
 32     string str_buffer, str_pattern;
 33     cin >> str_pattern >> str_buffer;
 34     StringPattern pattern(str_pattern);
 35     int location = pattern.match(str_buffer);
 36     if (location != -1) {
 37         cout << "match success, location is " << location << endl;
 38     } else {
 39         cout << "match failed!" << endl;
 40     }
 41     return 0;
 42 }
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 //KMP
 53 #include <iostream>
 54 #include <string>
 55 using std::cin;
 56 using std::cout;
 57 using std::endl;
 58 using std::string;
 59 
 60 class StringPattern {
 61 private:
 62     string value;
 63     //next记录每个char的上一个匹配位置
 64     int *next;
 65 public:
 66     StringPattern(const string &input) {
 67         value = input;
 68         next = new int[input.length() + 1];
 69         get_next();
 70     }
 71     ~StringPattern() {
 72         delete[] next;
 73     }
 74     void get_next() {
 75         next[0]=-1;
 76         //第一个next记为-1,循环求之后的next
 77         for(int i=1,match=-1;i<value.length();i++){
 78             //如果当前match>-1但不匹配,则回到上一个匹配点,直到成功匹配或者回到-1
 79             while(match>=0&&value[match+1]!=value[i]){
 80                 match=next[match];
 81             }
 82             //成功匹配
 83             if(value[match+1]==value[i]){
 84                     match++;
 85                 }
 86             next[i]=match;
 87         }
 88     }
 89     //在buffer中找value
 90     int match(const string &buffer) const {
 91         //注意buffer的每一位都要检查
 92         for(int i=0,match=-1;i<buffer.length();i++){
 93             //如果当前match>-1但不匹配,则回到上一个匹配点,直到成功匹配或者回到-1
 94             while(match>=0&&value[match+1]!=buffer[i]){
 95                 match=next[match];
 96             }
 97             if(value[match+1]==buffer[i]){
 98                 match++;
 99             }
100             if(match==value.length()-1){
101                 //这里也可以先不返回,改成记录下所有的匹配位置
102                 return i-match;
103             }
104         }
105         //匹配失败
106         return -1;
107     }
108 };
109 
110 int main() {
111     string str_buffer, str_pattern;
112     cin >> str_buffer >> str_pattern;
113     StringPattern pattern(str_pattern);
114     int location = pattern.match(str_buffer);
115     if (location != -1) {
116         cout << "match success, location is " << location << endl;
117     } else {
118         cout << "match failed!" << endl;
119     }
120     return 0;
121 }

 

posted @ 2017-07-16 23:29  NoviScl  阅读(167)  评论(0编辑  收藏  举报