-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn647_countSubstrings.cpp
More file actions
67 lines (62 loc) · 1.84 KB
/
n647_countSubstrings.cpp
File metadata and controls
67 lines (62 loc) · 1.84 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
647 计数多少个回文子串,具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
动态规划
dp[i][j]表示 [i ,j]字符串能不能构成回文串,那么dp[i][j] = dp[i +1][j - 1] && (s[i] == s[j])
*/
int countSubstrings(string s) {
int n = s.size(), ans = 0;
vector<vector<bool>> dp(n,vector<bool>(n,0));
for(int j=0;j<n;j++){
for(int i=0;i<=j;i++){
if(s[i]==s[j] && (j-i<2 || dp[i+1][j-1])){ //[i+1,j-1]=> i+2<=j
dp[i][j] = true;
ans++;
}
}
}
return ans;
}
//中心拓展,枚举2n-1个可能的回文中心
int countSubstrings(string s) {
int n = s.size(), ans = 0;
for (int i = 0; i < 2 * n - 1; ++i) {
int l = i / 2, r = i / 2 + i % 2; //r其实只是 l 或 l+1
while (l >= 0 && r < n && s[l] == s[r]) {
--l;
++r;
++ans;
}
}
return ans;
}
//Manacher 算法
//利用对称性 用已知的回文串,增加起始匹配长度,复杂度降低至O(n)
class Solution {
public:
int countSubstrings(string s) {
int n = s.size();
string t = "$#";
for (const char &c: s) {
t += c;
t += '#';
}
n = t.size();
t += '!';
auto f = vector <int> (n);
int iMax = 0, rMax = 0, ans = 0;
for (int i = 1; i < n; ++i) {
// 初始化 f[i]
f[i] = (i <= rMax) ? min(rMax - i + 1, f[2 * iMax - i]) : 1;
// 中心拓展
while (t[i + f[i]] == t[i - f[i]]) ++f[i];
// 动态维护 iMax 和 rMax
if (i + f[i] - 1 > rMax) {
iMax = i;
rMax = i + f[i] - 1;
}
// 统计答案, 当前贡献为 (f[i] - 1) / 2 上取整
ans += (f[i] / 2);
}
return ans;
}
};