数据结构(十 八)之字符串类的实现

本文详细介绍了KMP算法的原理与应用,探讨了字符串类的设计与实现,包括成员函数、操作符重载及异常处理等内容。同时,深入解析了如何在目标字符串中查找子串,以及KMP算法在字符串类中的新功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1 字符串类的设计
1.1.1 继承关系图
在这里插入图片描述
1.1.2 字符串类的实现

1.1.3 注意事项

  • 无缝实现String对象和 char * 字符串的互操作
  • 操作符重载函数需要考虑是否支持const 版本
  • 通过C语言中的字符串函数实现String的成员函数

1.2字符串中常用的成员函数

1.2.1成员函数

成员函数功能描述
operator[]操作符重载函数,访问指定下标的字符
startWith(s)判断字符串是否以s开头
endOf(s)判断字符串是否以S结尾
insert(i,s)在字符串的位置i处插入s
trim()去掉字符串两端的空白

1.2.2操作符重载函数

    char& operator[](int i);
    char operator[](int i) const;
  • 注意事项
    • 当i的取值不合法时,抛出异常
    • 合法范围: ( 0 <= i) && (i < m_length)

1.2.3 函数声明

class String : public Object
{
protected:
   char* m_str;
   int m_length;

   void init(const char* s);
   bool equal(const char* l, const char* r, int len) const;
public:
    String();
    String(char c);
    String(const char* s);
    String(const String&s);

    int length() const;
    const char* str() const;

    bool operator == (const String& s)const;
    bool operator == (const char* s)const;
    bool operator != (const String& s)const;
    bool operator != (const char* s)const;
    bool operator > (const String& s)const;
    bool operator > (const char* s)const;
    bool operator < (const String& s)const;
    bool operator < (const char* s)const;
    bool operator >= (const String& s)const;
    bool operator >= (const char* s)const;
    bool operator <= (const String& s)const;
    bool operator <= (const char* s)const;

    String operator + (const String& s) const;
    String operator + (const char* s) const;
    String& operator += (const String& s);
    String& operator += (const char* s);

    String& operator = (const String& s);
    String& operator = (const char* s);
    String& operator = (char c);

    char& operator[](int i);
    char operator[](int i) const;

    bool startWith(const char* s) const;
    bool startWith(const String&s) const;

    bool endOf(const char* s) const;
    bool endOf(const String&s) const;

    String& insert(int i, const char* s);
    String& insert(int i, const String&s);

    String& trim();     // 为啥是返回引用,因为可以这样使用 s2.trim().str()


    ~String();

};

1.2.4 思考

  • 如何在目标字符重查找是否存在指定的子串 ?
String s = "zhangsan";
int pos = s.indexof("san"); // pos = 5 

1.3KMP 算法

1.3.1

  • 如何在目标字符重查找是否存在指定的子串 ?
    在这里插入图片描述

1.3.2 伟大的发现

  • 匹配失败时的右移位数与子串本身相关, 和目标串无关
  • 移动位数 = 已匹配的字符数 - 对应的部分匹配值
  • 任意子串都存在一个唯一的部分匹配值

1.3.3 部分匹配表示例

1234567
ABCDABD
0000120
  • 用法
    在这里插入图片描述
  • 第7 位 匹配失败----> 前6 位匹配成功----> 查表PMT[6]
    ----> 右移位数6 - PMT[6] = 6 - 2 = 4;
  • 当我们知道部分匹配表就可以很清楚的知道,当匹配失败的时候,我们可以知道跳过几个字符

1.3.4 部分匹配如何获取

  • 前缀: 除了最后一个字符以外,一个字符串的全部头部组合
  • 后缀: 除了第一个字符以外,一个字符串的全部尾部组合
  • 部分匹配值: 前缀和后缀最长共有元素的长度
  • 举例:
    在这里插入图片描述
  • 示例:
0字符前缀后缀交集匹配值
1A0
2ABAB0
3ABCA,ABC,BC0
4ABCDA,AB,ABCD,CD,BCD0
5ABCDAA,AB,ABC,ABCDBCDA,CDA,DA,AA1
6ABCDABA,AB,ABC,ABCD,ABCDABCDAB,CDAB,DAB,AB,BAB2
7ABCDABDA,AB,ABC,ABCD,ABCDA,ABCDAB省略0

1.3.5 部分匹配如何产生

  • 实现关键
    • PMT[1] =0 (下标为0 的元素匹配值为0)
    • 从第二个字符开始递推(从下标为1的字符开始递推)
    • 假设PMT[N] = PMT[n - 1] + 1 (·最长共有元素的长度
    • 当假设不成立,PMT[n]在PMT[n - 1]的基础上减小

1.4
1.4.1部分匹配表实现
在这里插入图片描述

  • 代码实现
int* make_pmt(const char* p)
{
    int len = strlen(p);
    int* ret = static_cast<int*> (malloc(sizeof(int) * len));
    if(ret != NULL)
    {
        int ll = 0;  // longest length
        ret[0] = 0;
        for(int i =1; i < len; i++)
        {
            while((ll > 0) && (p[ll] != p[i]))
            {
                ll = ret[ll - 1];
            }
            if(p[ll] == p[i])
            {
                ll++;
            }
            ret[i] = ll;
        }
    }

    return ret;
}
int main()
{
    // ababax
    int* pmt = make_pmt("ABCDABD");
    for(int i = 0; i < strlen("ABCDABD"); i++)
    {
        cout << i << " : " << pmt[i] << endl;
    }
    return 0;
}

1.4.2 KMP的实现
在这里插入图片描述

  • 代码实现
int kmp(const char* s, const char* p)
{
    int ret = -1;
    int sl =strlen(s);
    int pl =strlen(p);
    
    int * pmt = make_pmt(p);
    if((pmt != NULL) && (0 < pl) && ( pl < sl))
    {
        for(int i =0,j =0; i < sl; i++)
        {
            while((j > 0) && (s[i] != p[j]))
            {
                j = pmt[j - 1];
            }
            if(s[i] = p[j])
            {
                j++;
            }
            if( j == pl)
            {
                ret = i + 1 - pl;
                break;
            }
        
        }    
    }
    free(pmt);
    
    return ret; 

}

int main()
{
    cout << kmp("zhangsan","zhangsan") << endl;
    return 0;
}

1.5 KMP算法的应用
1.5.1 字符串类中的新功能

成员函数功能
indexOf(s)查找子串s在字符串中位置
remove(s)将字符串中的子串s删除
operator - (s)定义字符串减法
replace(s,t)将字符串中的子串替换为t
sub(i, len)从字符串中创建子串

参考一 : 狄泰软件课程

如有侵权:请联系邮箱 1986005934@qq.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值