-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1930.cpp
More file actions
61 lines (52 loc) · 1.66 KB
/
Copy path1930.cpp
File metadata and controls
61 lines (52 loc) · 1.66 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
class Solution {
public:
int countPalindromicSubsequence(const string &s) const {
static pair<int, int> pos[27];
memset(pos, 0xFF, sizeof(pos));
const int n = s.size();
for (int i = 0; i < n; i++) {
const int idx = s[i] & 0x1F;
if (pos[idx].first == -1) pos[idx].first = i;
pos[idx].second = i;
}
int res = 0;
for (int i = 1; i <= 26; i++) {
const auto [first, last] = pos[i];
if (first == -1) continue;
bitset<27> bs;
for (int j = first + 1; j < last; j++)
bs.set(s[j] & 0x1F);
res += bs.count();
}
return res;
}
};
// Improve constant factor by using a lot of memory
class Solution {
public:
int countPalindromicSubsequence(const string &s) const {
static int count[100001][27];
memset(count, 0x00, sizeof(int) * 27);
static pair<int, int> pos[27];
memset(pos, 0xFF, sizeof(pos));
const int n = s.size();
for (int i = 0; i < n; i++) {
const int idx = s[i] & 0x1F;
if (i != 0) memcpy(count[i], count[i - 1], sizeof(int) * 27);
count[i][idx]++;
if (pos[idx].first == -1) pos[idx].first = i;
pos[idx].second = i;
}
int res = 0;
for (int i = 1; i <= 26; i++) {
const auto [first, last] = pos[i];
if (first == -1) continue;
count[last][i]--;
for (int j = 1; j <= 26; j++) {
res += count[last][j] > count[first][j];
}
count[last][i]++;
}
return res;
}
};