-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidx64.hpp
124 lines (87 loc) · 1.91 KB
/
idx64.hpp
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
#pragma once
#include "list.hpp"
#include "subs.hpp"
#include "strs.hpp"
#include "graph.hpp"
namespace LCIndex64 {
/**
///////////// 641.
*/
/**
///////////// 642.
*/
/**
///////////// 643.
*/
/**
///////////// 644.
*/
/**
///////////// 645.
*/
/**
///////////// 646.
*/
/**
///////////// 647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings
even they consist of same characters.
Example 1:
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
The input string length won't exceed 1000.
*/
int countPalindromicSubstrings(std::string s) {
size_t len = s.size();
std::vector<std::vector<bool>> state(len, std::vector<bool>(len, false));
int res = len;
for (size_t i = 0; i < len; ++i) {
state[i][i] = true;
for (size_t j = 0; j < i; ++j) {
if (i == j + 1) {
state[j][i] = s[i] == s[j];
} else if (i > j + 1) {
state[j][i] = s[i] == s[j] && state[j + 1][i - 1];
}
if (state[j][i]) {
++res;
}
}
}
return res;
}
FTEST(test_countPalindromicSubstrings) {
auto t = [](const std::string& s) {
auto re = countPalindromicSubstrings(s);
LOG(INFO) << s << " palindroms count: " << re;
return re;
};
FEXP(t(""), 0);
FEXP(t("a"), 1);
FEXP(t("ab"), 2);
FEXP(t("aa"), 3);
FEXP(t("aaa"), 6);
FEXP(t("baa"), 4);
FEXP(t("aab"), 4);
FEXP(t("aba"), 4);
FEXP(t("abc"), 3);
FEXP(t("abab"), 6);
FEXP(t("ebababb"), 12);
}
/**
///////////// 648.
*/
/**
///////////// 649.
*/
/**
///////////// 650.
*/
}