串的实现,KMP算法匹配字符串

String.h

#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::String()
{
    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;
}
    原文作者:KMP算法
    原文地址: https://blog.csdn.net/qq_39384184/article/details/78965965
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞