Strings

- Reloaded -

20231019 校隊培訓簡報

32717 高翊恩

關於講師

  • OJ handle: Pring, PringDeSu
  • 左閉右開教
  • 終於沒有複賽燒雞了

舊版簡報

更詳盡的演算法解說

精美插圖

Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution

Hash

Hash

  • 把一個字串當作是一個超級大的數字
  • 利用「27進位法」,\(a\rightarrow1,b\rightarrow2,\dots\)
  • 因為會溢位所以取個模

當兩個字串所得的大數字相等時,這兩個字串一定相等。

當兩個字串所得的數字取模後相等,視為這兩個字串相等。

Facts

  • 算 hash 值:\(O(n)\)
  • 預處理(前綴和)後算任意子字串 hash 值:\(O(n),O(1)\)
  • 字串匹配問題:\(O(n)\)
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution

KMP

CPS

若一個字串有一組前後綴滿足他們兩個相同,則放入 CPS 集合中

\(s=abbabba\)

\(s.CPS=\{abbabba,abba,a,0\}\)

[0]

[1]

[2]

[3]

感性證明

\(s.CPS[2]=S.CPS[1].CPS[1]\)

\(\pi\)陣列

對一個字串的每一個前綴\(p_i\)都記錄\(p_i.CPS[1]\)的長度

0 "" -1
1 "a" 0
2 "ab" 0
3 "abb" 0
4 "abba" 1
5 "abbab" 2
6 "abbabb" 3
7 "abbabba" 4

製造\(\pi\)陣列

使用DP

\(\$\$\$\$\$\$\$\$\$\ \$\)

製造\(\pi\)陣列

\(\$\$\$\$\$\$\$\$\$\ \$\)

製造\(\pi\)陣列

\(\$\$\$\$\$\$\$\$\$\ \$\)

如果有那麼一個CPS的話…

製造\(\pi\)陣列

\(\$\$\$\$\$\$\$\$\$\ \$\)

如果有那麼一個CPS的話…

綠線一定會相等!

製造\(\pi\)陣列

\(\$\$\$\$\$\$\$\$\$\ \$\)

窮舉這些相等的綠線,看他們下一個字元是否相等

製造\(\pi\)陣列

\(\$\$\$\$\$\$\$\$\$\ \$\)

窮舉這些相等的綠線,看他們下一個字元是否相等

我們只要找最長的紅線,所以綠線從長窮舉到短,找到為止

製造\(\pi\)陣列

\(\$\$\$\$\$\$\$\$\$\ \$\)

窮舉這些相等的綠線,看他們下一個字元是否相等

我們只要找最長的紅線,所以綠線從長窮舉到短,找到為止

綠線是前一個字串的CPS[1], CPS[2], ...

如何找\(CPS\)

\(s.CPS[2]=s.CPS[1].CPS[1]\)

\(s.CPS[2].size()=\pi[\pi[s.size()]]\)

將綠線的長度利用\(j\)存起來

就可以透過\(j\leftarrow\pi[j]\)的方式迭代每個CPS

時間複雜度:均攤\(O(n)\)

CODE

void KMP(string s, int *pi) {
    int n = s.size();
    pi[0] = -1;
    pi[1] = 0;
    for (int i = 1; i < s.size(); i++) {
        int j = pi[i];
        while (j >= 0 && s[j] != s[i]) j = pi[j];
        pi[i + 1] = j + 1;
    }
}

字串匹配

\(aabaaabab\)

\(aabab\)

\(aab\)

\(aabab\)

字串匹配

\(aabaaabab\)

\(aabab\)

\(aab\)

\(aabab\)

每次配爛時,就跳到「當前字串」的CPS[1]繼續試試看

 

複雜度:\(O(n)\)

CODE

void matching(string s, string t, int *pi) {
    int now = 0;
    for (int i = 0; i < s.size(); i++) {
        if (now == t.size()) {
            cout << "MATCHED: pos " << i - t.size() << endl;
            now = pi[now];
        }
        while (now >= 0 && s[i] != t[now]) now = pi[now];
        now++;
    }
    if (now == t.size()) {
        cout << "MATCHED: pos " << s.size() - t.size() << endl;
    }
}
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution
Word Combinations
String Matching
Finding Borders
Finding Periods
Minimal Rotation
Longest Palindrome
Required Substring
Palindrome Queries
Finding Patterns
Counting Patterns
Pattern Positions
Distinct Substrings
Repeating Substring
String Functions
Substring Order I
Substring Order II
Substring Distribution

strings

By pringdesu

strings

  • 328