20231019 校隊培訓簡報

32717 高翊恩

關於講師

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

CSES string

 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

• 把一個字串當作是一個超級大的數字
• 利用「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

Z-algorithm

 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

Manacher

 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

CPS

$$s=abbabba$$

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

[0]

[1]

[2]

[3]

$$s.CPS[2]=S.CPS[1].CPS[1]$$

$$\pi$$陣列

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

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

製造$$\pi$$陣列

$$\\\\\\\\\\ \$$

如何找$$CPS$$

$$s.CPS[2]=s.CPS[1].CPS[1]$$

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

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$$

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

Trie

 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

Aho-Corasick Automaton

 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

SA

 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

LCP

 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

Lyndon Factorization

 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

By pringdesu

• 225