目录

String


Data Structure

String

$Structure\rightarrow list$

  1. linked String
  2. Arrayed String

General Concepts of String

  1. $Null\ String(\emptyset):Nothing\ in\ the\ string, the\ length\ is\ zero.$
  2. $Blank(Space)string:Only\ includes\ one\ or\ more\ blanks(spaces)in\ the\ string.$
  3. $Substring: sub-sequence\ of\ one\ string.$

ADT

{

ADT String {Data Object:D={ ai | ai∈CharacterSet,i=1,2,...n, n≥0 }
Data Relationship:R1={ < ai-1, ai > | ai-1, ai∈D,i=2,...,n }
Operation: ……

} ADT String

Operations

{

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    StrAssign (&T, chars) //string assignment
    Initialchars are character constant.
    Outputassign chars to T.

    StrCopy (&T, S) //String Copy
    InitialS string.
    OutputCopy S string to T.

    DestroyString (&S)
    InitialS String.
    OutputDestroy S.

    ClearString (&S)
    InitialS String.
    OutputChange S into Null string.

    StrEmpty (S)
    InitialS String.
    OutputIf S is null stringreturn TRUEotherwise, return FALSE.

    StrCompare (S, T) //String comparison
    InitialS and T Strings.
    OutputIf S>Tthe return value is greater than 0
    if S=Tthe return value is 0
    if S<Tthe return value is less than 0.

    StrLength (S) // get string length
    Initial S String.
    OutputThe number of characters.

    Concat(&T,S1,S2) //String Concatenation
    InitialString S1 and S2
    OutputConcatenate S1 and S2 and put the new string into T

    SubString (&Sub, S, pos, len) //Get substring
    InitialString S1 <= pos <= StrLength(S) and 0 <= len <= StrLength(S) - pos + 1
    OutputReturn substring of string, which from pos to pos + len - 1 position of S

    Index (S, T, pos) //String index
    InitialString S and TT is non-empty,1 <= pos <= StrLength(S)
    Output: If the there is a substring T in S,
    return the position of sub-string T occurs first time after pos, otherwise return 0.

    Replace (&S, T, V) //String replacement
    InitialString S, T and V, T is non-empty.
    OutputReplace non-overlapped substring T of S with V

    StrInsert (&S, pos, T)
    InitialString S and T1 <= pos <= StrLength(S)  1
    OutputInsert String T before the pos character of string S

    StrDelete (&S, pos, len)
    InitialString S 1 <= pos <= StrLength(S) - len + 1
    OutputDelete a sub-string with len length from pos character in string S.

}

The StrAssign,Strcopy,StrCompare,StrLength,Concat and SubString are called as Minimum Operation Set of string type.

That is, these operations can not be implemented with other string operations, But the other string operations can be implemented with these 6 basic string operations.

String Pattern Matching

ForceMatch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Rank ForceMatch(const String &P, const String &T) {
    Rank n = T.length(), i = 0;
    Rank m = P.length(), j = 0;
    while (i < n && j < m) {
        if (T[i] == P[j]) {
            i++;
            j++;
        } else {
            i -= j - 1;
            j = 0;
        }
    }
    return i - j;
}

$n = T.length(),and\ m = P.length(), T=O(mn)$

KMP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int *BuildNext(const String &P) {
    Rank m = P.length();
    int *N = new int[m]{};
    int t = -1, j = 0;
    N[0] = -1;
    while (j < m - 1)
        (t < 0 || P[j] == P[t]) ? N[++j] = ++t : t = N[t];
    return N;
}

Rank KMPFirst(const String &P, const String &T) {
    int *next = BuildNext(P);
    Rank n = T.length(), i = 0;
    Rank m = P.length(), j = 0;
    while (i < n && j < m) {
        if (j < 0 || T[i] == P[j]) {
            i++;
            j++;
        } else
            j = next[j];
    }
    delete[]next;
    return i - j;
}

$n = T.length(),and\ m = P.length(), T=O(m+n)$

Optimization KMP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int *buildNext(const String &P) {
    Rank m = P.length();
    int *N = new int[m]{};
    int t = -1, j = 0;
    N[0] = -1;
    while (j < m - 1) {
        if (t < 0 || P[j] == P[t]) {
            j++;
            t++;
            N[j] = P[j] == P[t] ? N[t] : t;
        } else
            t = N[t];
    }
    return N;
}

Rank KMPEnd(const String &P, const String &T) {
    int *next = buildNext(P);
    Rank n = T.length(), i = 0;
    Rank m = P.length(), j = 0;
    while (i < n && j < m) {
        if (j < 0 || T[i] == P[j]) {
            i++;
            j++;
        } else
            j = next[j];
    }
    delete[]next;
    return i - j;
}

supplement

for ppt use python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def KMP(Str: str):
    length = len(Str)
    Next = [0 for _ in range(length)]
    j, t = 0, -1
    Next[0] = -1
    while j < length - 1:
        if t < 0 or Str[j] == Str[t]:
        j = j + 1
        t = t + 1
        Next[j] = t
    else:
        t = Next[t]
    Next = list(map(lambda k: k + 1, Next))
    return Next

in list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def KMP(Str: list):
    _size = len(Str)
    Next = [0 for _ in range(_size)]
    j, t = 1, 0
    while j < Str[0]:
        if t == 0 or Str[j] == Str[t]:
            t = t + 1
            j = j + 1
            Next[j] = t
        else:
            t = Next[t]
    return Next