-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq5.cpp
70 lines (63 loc) · 1.26 KB
/
q5.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
static const auto __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
struct unit {
int start, len;
};
class Solution {
public:
string longestPalindrome(string s) {
int rstart(0), max(1);
list<unit> alive;
if (s.size() < 2) return s;
//first two char;
if (s[1] == s[0]) {
unit temp;
temp.start = 0;
temp.len = 2;
//append to alive;
alive.push_back(temp);
}
for (int i = 2; i<s.size(); ++i) {
//find in alive;
for (list<unit>::iterator it = alive.begin(); it != alive.end();) {
if (it->start - 1>-1&& s[i] == s[it->start - 1]) {
it->start -= 1;
it->len += 2;
++it;
}
else {
if (it->len>max) {
rstart = it->start;
max = it->len;
}
it=alive.erase(it);
if (alive.empty()) {
it = alive.end();
}
}
}
if (s[i] == s[i - 2]) {
unit temp;
temp.start = i - 2;
temp.len = 3;
//append to alive;
alive.push_back(temp);
}
if (s[i] == s[i - 1]) {
unit temp;
temp.start = i - 1;
temp.len = 2;
//append to alive;
alive.push_back(temp);
}
}
if ((!alive.empty())&&alive.begin()->len > max) {
rstart = alive.begin()->start;
max = alive.begin()->len;
}
return s.substr(rstart, max);
}
};