串的实现,KMP算法匹配字符串
2018-01-03 21:55
417 查看
String.h
String.cpp
#pragma once #ifndef STRING_H #define STRING_H #include<string> class String { public: String(); String(std::string str); String(char *str); ~String(); bool empty() const; int size() const; char get(int index) const; int indexof(char ch) const; String* insert(char ch); String* insert(std::string str); String* insert(char *str, int length); String* insert(int index,char ch); String* insert(int index,std::string str); String* insert(int index, char *str, int length); String* del(int index); void output() const; int match(std::string str) const; int match(char *str) const; private: int next(std::string str) const; int MaxLength; int StringSize; char *Element; }; #endif
String.cpp
#include "String.h" #include<stdio.h> #define X 1.8 String::Str 4000 ing() { MaxLength = 2; StringSize = 0; Element = new char[MaxLength]; Element[StringSize] = '\0'; } String::String(std::string str) { MaxLength = 2; StringSize = 0; Element = new char[MaxLength]; Element[StringSize] = '\0'; int _length_ = str.length(); for (int _index_ = 0; _index_ < _length_; _index_++) { insert(str[_index_]); } } String::String(char *str) { MaxLength = 2; StringSize = 0; Element = new char[MaxLength]; Element[StringSize] = '\0'; for (int _index_ = 0; str[_index_] != '\0'; _index_++) { insert(str[_index_]); } } String::~String() { delete[] Element; } bool String::empty() const { return StringSize == 0; } int String::size() const { return StringSize; } char String::get(int index) const { if (index > StringSize) { printf("out of range\n"); return -1; } return Element[index]; } int String::indexof(char ch) const { for (int _index_ = 0; _index_ < StringSize; _index_++) { if (Element[_index_] == ch) { return _index_; } } printf("target not found\n"); return -1; } String* String::insert(char ch) { if (StringSize + 1 == MaxLength) { MaxLength *= X; char *_Element_ = Element; Element = new char[MaxLength]; for (int _index_ = 0; _index_ < StringSize; _index_++) { Element[_index_] = _Element_[_index_]; } delete[] _Element_; } Element[StringSize] = ch; StringSize++; Element[StringSize] = '\0'; return this; } String* String::insert(std::string str) { int _length_ = str.length(); if (StringSize + _length_ >= MaxLength) { do { MaxLength *= X; } while (StringSize + _length_ >= MaxLength); char *_Element_ = Element; Element = new char[MaxLength]; for (int _index_ = 0; _index_ < StringSize; _index_++) { Element[_index_] = _Element_[_index_]; } delete[] _Element_; } for (int _index_ = 0; _index_ < _length_; _index_++, StringSize++) { Element[StringSize] = str[_index_]; } Element[StringSize] = '\0'; return this; } String* String::insert(char *str, int length) { if (StringSize + length >= MaxLength) { do { MaxLength *= X; } while (StringSize + length >= MaxLength); char *_Element_ = Element; Element = new char[MaxLength]; for (int _index_ = 0; _index_ < StringSize; _index_++) { Element[_index_] = _Element_[_index_]; } delete[] _Element_; } for (int _index_ = 0; _index_ < length; _index_++, StringSize++) { Element[StringSize] = str[_index_]; } Element[StringSize] = '\0'; return this; } String* String::insert(int index, char ch) { if (index > StringSize) { printf("out of range\n"); return this; } if (StringSize + 1 == MaxLength) { MaxLength *= X; char *_Element_ = Element; Element = new char[MaxLength]; for (int _index_ = 0; _index_ < index; _index_++) { Element[_index_] = _Element_[_index_]; } delete[] _Element_; } for (int _index_ = StringSize; _index_ > index; _index_--) { Element[_index_] = Element[_index_ - 1]; } Element[index] = ch; StringSize++; Element[StringSize] = '\0'; return this; } String* String::insert(int index, std::string str) { if (index > StringSize) { printf("out of range\n"); return this; } int _length_ = str.length(); if (StringSize + _length_ == MaxLength) { do { MaxLength *= X; } while (StringSize + _length_ >= MaxLength); char *_Element_ = Element; Element = new char[MaxLength]; for (int _index_ = 0; _index_ < index; _index_++) { Element[_index_] = _Element_[_index_]; } delete[] _Element_; } for (int _index_ = (StringSize + _length_ - 1); _index_ >(index + _length_ - 1); _index_--) { Element[_index_] = Element[_index_ - _length_]; } for (int _index_ = 0; _index_ < _length_; _index_++, StringSize++) { Element[index + _index_] = str[_index_]; } Element[StringSize] = '\0'; return this; } String* String::insert(int index, char *str, int length) { if (index > StringSize) { printf("out of range\n"); return this; } if (StringSize + length == MaxLength) { do { MaxLength *= X; } while (StringSize + length >= MaxLength); char *_Element_ = Element; Element = new char[MaxLength]; for (int _index_ = 0; _index_ < index; _index_++) { Element[_index_] = _Element_[_index_]; } delete[] _Element_; } for (int _index_ = (StringSize + length - 1); _index_ >(index + length - 1); _index_--) { Element[_index_] = Element[_index_ - length]; } for (int _index_ = 0; _index_ < length; _index_++, StringSize++) { Element[index + _index_] = str[_index_]; } Element[StringSize] = '\0'; return this; } String* String::del(int index) { if (index >= StringSize) { printf("out of range\n"); } else if (StringSize == int(MaxLength / X)) { MaxLength /= X; char *_Element_ = new char[MaxLength]; for (int _index_ = 0; _index_ < index; _index_++) { _Element_[_index_] = Element[_index_]; } for (int _index_ = index; _index_ < StringSize; _index_++) { _Element_[_index_] = Element[_index_ + 1]; } StringSize--; delete[] Element; Element = _Element_; } else { for (int _index_ = index; _index_ < StringSize; _index_++) { Element[_index_] = Element[_index_ + 1]; } StringSize--; } return this; } void String::output() const { if (empty()) { printf("string is empty\n"); return; } for (int _index_ = 0; Element[_index_] != '\0'; _index_++) { printf("%c ", Element[_index_]); } printf("\n"); } int String::match(std::string str) const { int _length_ = str.length(); int *_next_ = new int[_length_]; _next_[0] = 0; _next_[1] = 0; int _index_, _str_index_; for (_index_ = 2; _index_ < _length_; _index_++) { _next_[_index_] = next(str.substr(0, _index_)); } /* for (int i = 0; i < _length_; i++) { printf("%d ",_next_[i]); } printf("\n"); */ for (_index_ = 0, _str_index_ = 0; _index_ < StringSize&&_str_index_ < _length_; _index_++) { if (Element[_index_] == str[_str_index_]) { _str_index_++; continue; } else if (_str_index_ == _next_[_str_index_]) { continue; } else { _str_index_ = _next_[_str_index_]; _index_--; } } if (_str_index_ == _length_) { return _index_-_length_; } else { printf("target not found\n"); return -1; } } int String::match(char *str) const { std::string _str_(str); return match(_str_); } int String::next(std::string str) const { int _length_ = str.length(); int _index_, i, j; for (_index_ = 1; _index_ < _length_; _index_++) { for (i = 0, j = _index_; j < _length_; i++, j++) { if (str[i] != str[j]) { break; } } if (j == _length_) { return _length_ - _index_; } } return 0; }
相关文章推荐
- 字符串模式匹配中BF算法和KMP算法的java实现
- 字符串模式匹配之KMP算法图解与 next 数组原理和实现方案
- c/c++实现字符串模式匹配BM算法和KMP算法
- KMP算法实现字符串的模式匹配完整C代码
- KMP算法 字符串模式匹配
- KMP算法计算next数组 匹配字符串
- 利用Linux 自带的GNU正则表达式库实现查找字符串中全部匹配左右边界限定的子串
- KMP-字符串模式匹配(c++/python实现)
- java 算法实现字符串的匹配
- (6)KMP算法(求子串的位置)______字符串的匹配
- KMP字符串模式匹配算法实现
- java实现字符串的一般和KMP模式匹配算法
- 简单字符串模式匹配算法的C++实现
- 字符串模式匹配KMP算法
- 字符匹配KMP算法 java实现
- 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字
- 用IPTables实现字符串模式匹配
- 算法:KMP算法实现的模式匹配
- java实现子字符串的KMP算法
- 字符串模式匹配KMP算法