-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path730_Count_Different_Palindromic_Subsequences.cpp
416 lines (364 loc) · 15.3 KB
/
730_Count_Different_Palindromic_Subsequences.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <set>
#include <math.h>
using namespace std;
/**
* @brief The Solution class
Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.
A subsequence of a string S is obtained by deleting 0 or more characters from S.
A sequence is palindromic if it is equal to the sequence reversed.
Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.
Example 1:
Input:
S = 'bccb'
Output: 6
Explanation:
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.
Example 2:
Input:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.
Note:
The length of S will be in the range [1, 1000].
Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.
*/
/**
* 版本 1:
* 保存每种字母 (a,b,c,d) 出现的下标到 inds 数组,字母出现次数不为 0 时,总数加 1;字母出现次数大于 2 时,总数加 1,并选出两个字母用于构造回文串
* 思路:
* 1. 先选出两个相同的字母 str1(aa,bb,cc,dd),分别对应最小下标 start 和最大下标 end,即在字符串 S 中最靠边的位置
* 2. 回文串长度加 1:从 inds 中选择下标 index(index > start 且 index < end),在 str1(aa,bb,cc,dd) 中插入一个字母(a,b,c,d) 成为新的回文串 str2
* str2 相当于在 start 和 end 直接再选择一个字母成为新的回文串
* 3. 回文串长度加 2:从 inds 中选择下标 ss(ss > start) 和 ee(ee < end),在 str1(aa,bb,cc,dd) 中插入两个相同字母(aa,bb,cc,dd) 成为新的回文串 str3
* str3 相当于在 start 和 end 直接再选择两个相同的字母成为新的回文串
* 4. 处理回文串 str3,更新 start=ss 和 end=ee,长度分别加 1 和加 2,重复上述流程
* 代码解释:
* const int DIFF_CHAR = 4;//有 4 种字母
* array<vector<int>, DIFF_CHAR> inds; //保存 4 种字母出现的下标,即预处理 S,保存对应的字母和下标
* int sum = 0; //保存回文串的总数
* vector<pair<int, int> > res; //pair 保存了每次处理回文串的 start 和 end,开始为空
* 问题:
* 因为没有保存中间值(即 start 和 end 相同),重复计算导致超时
*/
int countPalindromicSubsequences1(string S) {
const int DIFF_CHAR = 4;
array<vector<int>, DIFF_CHAR> inds;
for(size_t i=0; i<S.length(); i++)
inds.at(S.at(i)-'a').push_back(i);
int sum = 0;
vector<pair<int, int> > res;
for(size_t i=0; i<DIFF_CHAR; i++)
{
if(!inds.at(i).empty())
{
sum++;
}
if(inds.at(i).size() > 1)
{
// save the first and last index of the Palindromic Subsequences
sum++;
res.emplace_back(inds.at(i).front(), inds.at(i).back());
}
}
if(res.empty()) return sum;
while(!res.empty())
{
int start = res.back().first;
int end = res.back().second;
res.pop_back();
for(size_t j=0; j<DIFF_CHAR; j++)
{
vector<int> vec2 = inds.at(j);
//add length by 1 for pairs
for(auto it : vec2)
{
if(it >= end) break;
if(it > start && it < end)
{
sum++;
break;
}
}
//add length by 2 for pairs
bool b1=false, b2=false;
int ss=-1, ee=-1;
for(auto it=vec2.begin(); it!=vec2.end(); it++)
{
if(*it >= end) break;
if(!b1 && *it > start)
{
b1 = true;
ss = *it;
for(auto itt=vec2.rbegin(); *itt!=*it; itt++)
{
if(*itt <= start) break;
if(!b2 && *itt < end)
{
b2 = true;
ee = *itt;
break;
}
}
break;
}
}
if(b1 && b2)
{
sum++;
res.emplace_back(ss, ee);
}
}
}
return sum;
}
/**
* 版本 2:
* 添加数组保存中间值(即 start 和 end 相同)
* 思路:
* 实现回调函数求下标范围 [start, end] 对应的回文子串数目,每次更新数组记录中间值
* 代码解释:
* array<set<int>, 4> m_inds; //保存 4 种字母出现的下标,即预处理 S,保存对应的字母和下标
* int m_sumInds[4][1001]; //保存 4 种字母在 S 中从开始到某个位置出现的次数,用于查找在某个范围内是否出现,可快速筛查是否有满足要求的字母可以用于构造新的回文串,初始化为 0
* int m_sumSub[1001][1001]; //保存中间值,对应 S 任意两个位置 [start, end] 对应的回文串数目,初始化为 -1
* //回文串的总数即 m_sumSub[1][S.length()]
* 问题:
* 使用 set::upper_bound 可以快速求出满足范围的新的 ss,但是查找对应的 ee 还需要遍历数组
*/
class Solution2 {
public:
const int N_MODE = pow(10, 9) + 7;
size_t m_lenStr = -1;
array<set<int>, 4> m_inds;
int m_sumInds[4][1001];
int m_sumSub[1001][1001];
int countPalindromicSubsequences(string S) {
m_lenStr = S.length();
for(size_t i=0; i<4; i++)
for(size_t j=0; j<=m_lenStr; j++)
m_sumInds[i][j] = 0;
for(size_t i=0; i<=m_lenStr; i++)
for(size_t j=0; j<=m_lenStr; j++)
m_sumSub[i][j] = -1;
for(size_t i=0; i<m_lenStr; i++)
{
int index = S.at(i)-'a';
m_inds.at(index).emplace(i+1);
m_sumInds[index][i+1] = m_sumInds[index][i] + 1;
}
for(size_t i=0; i<4; i++)
for(size_t j=1; j<=m_lenStr; j++)
m_sumInds[i][j] = m_sumInds[i][j-1] + m_sumInds[i][j];
int sum = 0;
for(size_t i=0; i<4; i++)
{
if(m_sumInds[i][m_lenStr] != 0)
{
sum = (sum + 1) % N_MODE;
if(m_sumInds[i][m_lenStr] > 1)
{
int ss = *m_inds.at(i).begin();
int ee = *m_inds.at(i).rbegin();
sum = (sum + SubOfString(ss, ee)) % N_MODE;
}
}
}
return sum;
}
int SubOfString(int start, int end)
{
if(start < 0 || end < 0) return 0;
if(m_sumSub[start][end] != -1) return m_sumSub[start][end];
int sum = 1;
for(size_t i=0; i<4; i++)
{
if(m_sumInds[i][end-1] != m_sumInds[i][start])
{
sum = (sum + 1) % N_MODE;//add length by 1 for pairs
if(m_sumInds[i][end-1] - m_sumInds[i][start] > 1)//add length by 2 for pairs
{
int ss = *m_inds.at(i).upper_bound(start);
for(int ee=end-1; ee>ss; ee--)
{
if(m_sumInds[i][ee] != m_sumInds[i][ee-1])
{
sum = (sum + SubOfString(ss, ee)) % N_MODE;
break;
}
}
}
}
}
m_sumSub[start][end] = sum;
return m_sumSub[start][end];
}
};
/**
* 版本 3:
* 优化查找满足要求的 ss 和 ee 的过程
* 构造思路:
* 添加数组保存字母在某个位置之前和之后最近出现的下标
* 代码解释:
* int m_lowerInds[4][1001]; //保存字母在某个位置之后最近出现的下标,初始化为 0
* int m_upperInds[4][1001]; //保存字母在某个位置之前最近出现的下标,初始化为 0
* int m_sumSub[start][end]; //保存下标从 start 到 end 的子串有多少个回文串,初始化为 -1
* 关于 Solution3
* Solution3 使用 vector 替换数组,初始化速度更快,但是内存占用(39-40MB)相比数组(12-13MB)增大
* 举例:
* S = 'bccb'
* m_lowerInds['b'-'a'][1001]={1,4,4,4}, m_upperInds['b'-'a'][1001]={1,1,1,4}
* m_lowerInds['c'-'a'][1001]={2,2,3,0}, m_upperInds['c'-'a'][1001]={0,2,3,3}
* m_lowerInds[i][1]) 对应字母 'a'+i 在 S 中第一次出现的下标
* m_upperInds[i][m_lenStr] 对应字母 'a'+i 在 S 中最后一次出现的下标
* m_lowerInds[i][start]) 对应字母 'a'+i 在 S 中 start 之后(包括 start)第一次出现(最靠近 start)的下标
* m_upperInds[i][end] 对应字母 'a'+i 在 S 中 end 之前(包括 end)第一次出现(最靠近 end)的下标
*/
class Solution {
public:
const int N_MODE = 1e9 + 7;
int m_lowerInds[4][1001];
int m_upperInds[4][1001];
int m_sumSub[1001][1001];
inline int myMod(int num) {return num < N_MODE ? num : num - N_MODE;}
int countPalindromicSubsequences(string S) {
size_t lenStr = S.length();
for(size_t i=0; i<4; i++)
memset(m_lowerInds[i], 0, sizeof(int)*(lenStr+1));
for(size_t i=0; i<4; i++)
memset(m_upperInds[i], 0, sizeof(int)*(lenStr+1));
for(size_t i=0; i<=lenStr; i++)
memset(m_sumSub[i], -1, sizeof(int)*(lenStr+1));
for(size_t i=0; i<lenStr; i++)
m_lowerInds[S.at(i)-'a'][i+1] = m_upperInds[S.at(i)-'a'][i+1] = i + 1;
for(size_t i=0; i<4; i++)
for(size_t j=lenStr-1; j>=1; j--)
if(m_lowerInds[i][j] == 0)
m_lowerInds[i][j] = m_lowerInds[i][j+1];
for(size_t i=0; i<4; i++)
for(size_t j=2; j<=lenStr; j++)
if(m_upperInds[i][j] == 0)
m_upperInds[i][j] = m_upperInds[i][j-1];
int sum = 0;
for(size_t i=0; i<4; i++)
{
if(m_upperInds[i][lenStr] != 0)
{
sum = myMod(sum + 1);
if(m_upperInds[i][lenStr] != m_lowerInds[i][1])
{
int ss = m_lowerInds[i][1];
int ee = m_upperInds[i][lenStr];
sum = myMod(sum + SubOfString(ss, ee));
}
}
}
return sum;
}
int SubOfString(int start, int end)
{
if(start < 0 || end < 0) return 0;
if(m_sumSub[start][end] != -1) return m_sumSub[start][end];
int sum = 1;
for(size_t i=0; i<4; i++)
{
if(m_upperInds[i][end-1] > start)
{
sum = myMod(sum + 1);//add length by 1 for pairs
if(m_upperInds[i][end-1] > m_lowerInds[i][start+1])//add length by 2 for pairs
{
int ss = m_lowerInds[i][start+1];
int ee = m_upperInds[i][end-1];
sum = myMod(sum + SubOfString(ss, ee));
}
}
}
m_sumSub[start][end] = sum;
return m_sumSub[start][end];
}
};
class Solution3 {
public:
const int N_MODE = 1e9 + 7;
vector<vector<int>> m_lowerInds;
vector<vector<int>> m_upperInds;
vector<vector<int>> m_sumSub;
inline int myMod(int num) {return num < N_MODE ? num : num - N_MODE;}
int countPalindromicSubsequences(string S) {
size_t lenStr = S.length();
m_lowerInds.resize(4, vector<int>(lenStr+1, 0));
m_upperInds.resize(4, vector<int>(lenStr+1, 0));
m_sumSub.resize(lenStr+1, vector<int>(lenStr+1, -1));
for(size_t i=0; i<lenStr; i++)
m_lowerInds[S.at(i)-'a'][i+1] = m_upperInds[S.at(i)-'a'][i+1] = i + 1;
for(size_t i=0; i<4; i++)
for(size_t j=lenStr-1; j>=1; j--)
if(m_lowerInds[i][j] == 0)
m_lowerInds[i][j] = m_lowerInds[i][j+1];
for(size_t i=0; i<4; i++)
for(size_t j=2; j<=lenStr; j++)
if(m_upperInds[i][j] == 0)
m_upperInds[i][j] = m_upperInds[i][j-1];
int sum = 0;
for(size_t i=0; i<4; i++)
{
if(m_upperInds[i][lenStr] != 0)
{
sum = myMod(sum + 1);
if(m_upperInds[i][lenStr] != m_lowerInds[i][1])
{
int ss = m_lowerInds[i][1];
int ee = m_upperInds[i][lenStr];
sum = myMod(sum + SubOfString(ss, ee));
}
}
}
return sum;
}
int SubOfString(int start, int end)
{
if(start < 0 || end < 0) return 0;
if(m_sumSub[start][end] != -1) return m_sumSub[start][end];
int sum = 1;
for(size_t i=0; i<4; i++)
{
if(m_upperInds[i][end-1] > start)
{
sum = myMod(sum + 1);//add length by 1 for pairs
if(m_upperInds[i][end-1] > m_lowerInds[i][start+1])//add length by 2 for pairs
{
int ss = m_lowerInds[i][start+1];
int ee = m_upperInds[i][end-1];
sum = myMod(sum + SubOfString(ss, ee));
}
}
}
m_sumSub[start][end] = sum;
return m_sumSub[start][end];
}
};
int main()
{
vector<pair<string, int>> test;
test.emplace_back("a", 1);
test.emplace_back("aaa", 3);
test.emplace_back("bccb", 6);
test.emplace_back("abccba", 14);
test.emplace_back("abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba", 104860361);
test.emplace_back("bcbacbabdcbcbdcbddcaaccdcbbcdbcabbcdddadaadddbdbbbdacbabaabdddcaccccdccdbabcddbdcccabccbbcdbcdbdaada", 117990582);
test.emplace_back("dcdacabbabacbddababcdabdaaddcdcadbbaccbadcacdbddaabdabbcdacbcdadbbdccadacbbabacaccbccadcccacbacbcacccdaacacabbbcbaabaadbbbdcdcbdcadabdcdacbadabcaaacdbbbcadadcaadcabcdcdcbaccccdabbbdccdcbcaaaaccabdbdadcdbcbbcddcaabcbbcbabbabcbdbcdabadabccabbaabbdcbbccbabdccacacccbdabbccdbccdaaabddbccbbddaabacacbbbdacbcbabccdcaddccbcdbbacbbadcccccbaddddcdbdadcbcdcdbddacbbcdcdaddadbccaccbadcbcccacbddabbddbaabddccbddbbdccaacadbdcdbaaacbbbcabcdcbaaccbcdcdbacbddccbcbbbaadccdabacaccbbdabddbacbabcbcacddddcdbdcaaabbbaaacdbcdcabbbcbacbacadbacdcdcabacbaabdabccaccbadabbcdccbdbcccbddcdaaadadccccbabcddbacbadbbbcaaaddcacddbcaadaaadbdcbdbbbccaabbdbdbdccbbaddaacabbcbbbdccdaabbcaccabacabccccbbbdcadabbcaaacdbcdabdaaaacbdbddadaaadaccabcaaadccaddbbcdcddadbdbdcbddbadddbbabdddbdcacbcaadaddbdcacbcdcdccdddaadccbabbbdbcdacbbdbadacbaaddcbacdaddbbbacacbcdbccddcaadbcccdbcaaaadcdbcddddbacadbdcdaccddacbdcabbbaababdaddccddaacbbbdcdcdbdddcabdbaaccdbddccacdcdaaaddccccddcbadcddddacddadcbdbccbbadcbbdbcbbbabddbcccacbadacbcccdcdbbbdcdcbbac", 480484038);
int mod = pow(10, 9) + 7;
for(auto it : test)
{
string str = it.first;
Solution solu;
int ans = it.second;
int res = solu.countPalindromicSubsequences(str);
cout << "result=" << res << "; module=" << res % mod << "; answer=" << ans << endl;
}
return 0;
}