Data structure string

Keywords: Algorithm data structure

1. DS string application - KMP algorithm

Title Description

Learn KMP algorithm, give the main string and mode string, and find the position of mode string in the main string

The algorithm framework is as follows for reference only

input

The first input t indicates that there are t instances

The second line inputs the main string of the first instance, and the third line inputs the mode string of the first instance

and so on

output

The first line outputs the next value of the pattern string of the first instance

The second line outputs the matching position of the first instance. The position is calculated from 1. If the matching succeeds, the position is output, and if the matching fails, 0 is output

and so on

sample input

3
qwertyuiop
tyu
aabbccdd
ccc
aaaabababac
abac

sample output

-1 0 0
5
-1 0 1
0
-1 0 0 1
8

Reference code

#include <iostream>
#include <string>
using namespace std;
class myString
{
private:
    string mainstr;
    int size;
    void getnext(string p, int next[])
    {
        int j = 0;
        int k = -1;
        next[j] = k;
        int len = p.length();
        while (j < len - 1)
        {
            if (k == -1 || p[j] == p[k])
            {
                j++;
                k++;
                next[j] = k;
            }
            else
            {
                k = next[k];
            }
        }
    }
    int KMPfind(string p, int pos, int next[])
    {
        int i = pos;
        int j = 0;
        int len = p.length();
        while (i < size && j < len)
        {
            if (j == -1 || mainstr[i] == p[j])
            {
                i++;
                j++;
            }
            else
            {
                j = next[j];
            }
        }
        if (j >= len)
            return i - len;
        else
        {
            return -1;
        }
    }

public:
    myString()
    {
        size = 0;
        mainstr = "";
    }
    ~myString()
    {
        size = 0;
        mainstr = "";
    }
    void setval(string sp)
    {
        mainstr.assign(sp);
        size = mainstr.length();
    }
    int KMPfindSubstr(string p, int pos)
    {
        int i;
        int L = p.length();
        int *next = new int[L];
        getnext(p, next);
        for (i = 0; i < L; i++)
        {
            cout << next[i] << ' ';
        }
        cout << endl;
        int v = -1;
        v = KMPfind(p, pos, next);
        delete[] next;
        return v + 1;
    }
};
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string a, b;
        cin >> a >> b;
        myString p;
        p.setval(a);
        cout << p.KMPfindSubstr(b, 0) << endl;
    }
    return 0;
}

2. DS string application - string replacement

Title Description

The main string, mode string and replacement string are given. The KMP algorithm is used to find the position of the mode string in the main string, and then the mode string is replaced with the character of the replacement string

This question only considers the situation of one replacement. If you want to be perfect, you can realize multiple replacement

It may be necessary to consider the inconsistency between the length of the pattern string and the replacement string

input

Enter n in the first line to indicate the number of customers

The second line enters the type of each customer, separated by spaces

In the third line, enter the processing time of each customer, separated by spaces

output

The first line outputs the main string of the first instance

The second line outputs the result of the main string replacement of the first instance. If there is no replacement, the original content of the main string is output.

and so on

sample input

3
aabbccdd
bb
ff
aaabbbccc
ddd
eee
abcdef
abc
ccccc

sample output

aabbccdd
aaffccdd
aaabbbccc
aaabbbccc
abcdef
cccccdef

Reference code

#include <iostream>
#include <string>
using namespace std;
class myString
{
public:
    string mainstr;
    int size;
    void getnext(string p, int next[])
    {
        int j = 0;
        int k = -1;
        next[j] = k;
        int len = p.length();
        while (j < len - 1)
        {
            if (k == -1 || p[j] == p[k])
            {
                j++;
                k++;
                next[j] = k;
            }
            else
            {
                k = next[k];
            }
        }
    }
    int KMPfind(string p, int pos, int next[])
    {
        int i = pos;
        int j = 0;
        int len = p.length();
        while (i < size && j < len)
        {
            if (j == -1 || mainstr[i] == p[j])
            {
                i++;
                j++;
            }
            else
            {
                j = next[j];
            }
        }
        if (j >= len)
            return i - len;
        else
        {
            return -1;
        }
    }

    myString()
    {
        size = 0;
        mainstr = "";
    }
    ~myString()
    {
        size = 0;
        mainstr = "";
    }
    void setval(string sp)
    {
        mainstr.assign(sp);
        size = mainstr.length();
    }
    int KMPfindSubstr(string p, int pos)
    {
        int i;
        int L = p.length();
        int *next = new int[L];
        getnext(p, next);
        int v = -1;
        v = KMPfind(p, pos, next);
        delete[] next;
        return v + 1;
    }
    void replace(string p, int pos,string c)
    {
        string result = "";
        result += mainstr.substr(0, pos-1);
        result += p;
        result += mainstr.substr(pos-1+c.length(), mainstr.length());
        mainstr = result;
    }
};
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string a, b, c;
        cin >> a >> b >> c;
        myString p;
        p.setval(a);
        cout << p.mainstr << endl;
        int num = p.KMPfindSubstr(b, 0);
        if (num != 0)
            p.replace(c, num,b);
        cout << p.mainstr << endl;
    }
}

3. DS string application - longest repeating substring

Title Description

Find the longest repeated substring length of the string (substrings do not overlap). For example, the longest repeating substring of abcaefabcabc is the string abca, with a length of 4.

input

Test times t

t test strings

output

For each test string, output the longest repeated substring length. If there is no repeated substring, output - 1

sample input

3
abcaefabcabc
szu0123szu
szuabcefg

sample output

4
3
-1

Reference code

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s, subr, subl;
    int t;
    int i, flag, j;
    cin >> t;
    while (t--)
    {
        cin >> s;
        int len = s.length();
        for (i = len / 2, flag = 0; i >= 1 && !flag; i--)
            for (j = 0; j < len - 2 * i; j++)
            {
                subl = s.substr(j, i);
                subr = s.substr(i + j);
                if (subr.find(subl) != string::npos)
                {
                    cout << i << endl;
                    flag = 1;
                    break;
                }
            }
        if (!flag)
            cout << -1 << endl;
    }
    return 0;
}

Posted by 87dave87 on Tue, 07 Sep 2021 11:16:18 -0700