- Reloaded -
20231019 校隊培訓簡報
32717 高翊恩
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 |
若一個字串有一組前後綴滿足他們兩個相同,則放入 CPS 集合中
\(s=abbabba\)
\(s.CPS=\{abbabba,abba,a,0\}\)
[0]
[1]
[2]
[3]
\(s.CPS[2]=S.CPS[1].CPS[1]\)
對一個字串的每一個前綴\(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 |
使用DP
\(\$\$\$\$\$\$\$\$\$\ \$\)
\(\$\$\$\$\$\$\$\$\$\ \$\)
\(\$\$\$\$\$\$\$\$\$\ \$\)
如果有那麼一個CPS的話…
\(\$\$\$\$\$\$\$\$\$\ \$\)
如果有那麼一個CPS的話…
綠線一定會相等!
\(\$\$\$\$\$\$\$\$\$\ \$\)
窮舉這些相等的綠線,看他們下一個字元是否相等
\(\$\$\$\$\$\$\$\$\$\ \$\)
窮舉這些相等的綠線,看他們下一個字元是否相等
我們只要找最長的紅線,所以綠線從長窮舉到短,找到為止
\(\$\$\$\$\$\$\$\$\$\ \$\)
窮舉這些相等的綠線,看他們下一個字元是否相等
我們只要找最長的紅線,所以綠線從長窮舉到短,找到為止
綠線是前一個字串的CPS[1], CPS[2], ...
\(s.CPS[2]=s.CPS[1].CPS[1]\)
\(s.CPS[2].size()=\pi[\pi[s.size()]]\)
將綠線的長度利用\(j\)存起來
就可以透過\(j\leftarrow\pi[j]\)的方式迭代每個CPS
時間複雜度:均攤\(O(n)\)
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)\)
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 |