KMP 算法的應用(二十七)

        我們在上節博客中講到了 KMP 算法的具體實現,那麼我們本節就來看看 KMP 算法的應用。問題:如何在目標字符串中查找是否存在指定的子串

        我們來看看字符串類中的新功能,如下圖所示

圖片.png

       1、子串查找(KMP  算法的直接應用)

            int indexOf(const char* s) const;

            int indexOf(const String& s) const;

        我們來看看具體源碼的實現,如下

int String::indexOf(const char* s) const
{
    return kmp(m_str, s ? s : "");
}

int String::indexOf(const String& s) const
{
    return kmp(m_str, s.m_str);
}

        直接利用我們上節博客實現的 kmp 函數就能實現 indexOf 函數,我們來看看效果

#include <iostream>
#include <cstring>
#include "DTString.h"

using namespace std;
using namespace DTLib;

int main()
{
    String s = "ababax";

    cout << s.indexOf("bax") << endl;

    return 0;
}

        編譯運行結果如下

圖片.png

        我們看到返回的位置爲下標爲 3 處,也就是 s[3] 處。

        2、在字符串中將指定的子串刪除

            String& remove(int i, int len);

            String& remove(const char* s);

            String& remove(const String& s);

        根據 KMP 在目標字符串中查找子串的位置,再通過子串位置和子串長度進行刪除。具體源碼實現如下

String& String::remove(int i, int len)
{
    if( (0 <= i) && (i < m_length) )
    {
        int n = i;
        int m = i + len;

        while( (n < m) && (m < m_length) )
        {
            m_str[n++] = m_str[m++];
        }

        m_str[n] = '\0';
        m_length = n;
    }

    return *this;
}

String& String::remove(const char* s)
{
    return remove(indexOf(s), s ? strlen(s) : 0);
}

String& String::remove(const String& s)
{
    return remove(indexOf(s), s.length());
}

        我們來寫個測試代碼進行測試,如下

#include <iostream>
#include <cstring>
#include "DTString.h"

using namespace std;
using namespace DTLib;

int main()
{
    String s = "ababax";

    cout << s.remove("bab").str() << endl;

    return 0;
}

        我們來看看結果是不是 aax,如下

圖片.png

        3、字符串的減法操作定義(operator -)

        使用 remove 實現字符串間的減法操作,必須得保證字符串自身不被修改,返回產生的新串。最後效果如下

圖片.png

            String operator - (const String& s) const;

            String operator - (const char* s) const;

            String& operator -= (const String& s);

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

        利用原有的 + 操作符進行改裝即可,源碼實現如下

String String::operator - (const String& s) const
{
    return String(*this).remove(s);
}

String String::operator - (const char* s) const
{
    return String(*this).remove(s);
}

String& String::operator -= (const String& s)
{
    return remove(s);
}

String& String::operator -= (const char* s)
{
    return remove(s);
}

        我們來測試下,測試代碼如下

#include <iostream>
#include <cstring>
#include "DTString.h"

using namespace std;
using namespace DTLib;

int main()
{
    String s = "ababax";

    String s1 = s - "bax";

    cout << s.str() << endl;
    cout << s1.str() << endl;

    s -= s;

    cout << "[" << s.str() << "]" << endl;

    return 0;
}

        我們來看看運行結果,s1 = "aba", 最後的 s 應該爲空,我們來看看結果

圖片.png

        我們看到結果正如我們所分析的那樣。

        4、字符串中的子串替換

            String& replace(const char* t, const char* s);

            String& replace(const String& t, const char* s);

            String& replace(const char* t, const String& s);

            String& replace(const String& t, const String& s);

        我們來看看具體源碼的實現

String& String::replace(const char* t, const char* s)
{
    int index = indexOf(t);

    if( index > 0 )
    {
        remove(t);
        insert(index, s);
    }

    return *this;
}

String& String::replace(const String& t, const char* s)
{
    return replace(t.m_str, s);
}

String& String::replace(const char* t, const String& s)
{
    return replace(t, s.m_str);
}

String& String::replace(const String& t, const String& s)
{
    return replace(t.m_str, s.m_str);
}

        我們來寫個測試代碼進行測試,代碼如下

#include <iostream>
#include <cstring>
#include "DTString.h"

using namespace std;
using namespace DTLib;

int main()
{
    String s = "ababax";

    s.replace("baba", "xyz");

    cout << s.str() << endl;

    return 0;
}

        我們來看看最後的結果是不是 axyzx,運行結果如下

圖片.png

        5、從字符串中創建子串

            String sub(int i, int len) const;

        以 i 爲起點提取長度爲 len 的子串,子串提取保證不會改變字符串本身的狀態。如下

圖片.png

        具體源碼實現如下

String String::sub(int i, int len) const
{
    String ret;

    if( (0 <= i) && (i < m_length) )
    {
        if( len < 0 ) len = 0;
        if( len > m_length ) len = m_length - i;
        char* str = static_cast<char*>(malloc(len + 1));

        strncpy(str, m_str + i, len);

        str[len] = '\0';

        ret = str;
    }
    else
    {
        THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
    }

    return ret;
}

        我們來寫個測試代碼進行測試,測試代碼如下

#include <iostream>
#include <cstring>
#include "DTString.h"

using namespace std;
using namespace DTLib;

int main()
{
    String s = "ababax";
    String s1 = s.sub(3, 10);
    String s2 = s.sub(2, 3);

    cout << s.str() << endl;
    cout << s1.str() << endl;
    cout << s2.str() << endl;

    return 0;
}

        最後的結果應該是 s = "ababax" ; s1 = "bax" ; s3 = "aba" ; 運行結果如下

圖片.png

        現在我們的字符串類已經實現的有點規模了。通過今天對字符串類的學習,總結如下:1、字符串類是工程開發中必不可少的組件;2、字符串中應該包含常用的字符串操作函數,a> 增:insert, operator + , ...; b> 刪:remove,operator -,...; c> 查:indexOf,...; d> 改:replace,...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章