-
Notifications
You must be signed in to change notification settings - Fork 0
/
200326-2.cpp
60 lines (57 loc) · 1.45 KB
/
200326-2.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
// https://leetcode-cn.com/problems/shortest-palindrome/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
string shortestPalindrome(string s) {
vector<pair<char, int>> a;
char last = 0;
int count = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] != last) {
if (count > 0) a.push_back(make_pair(last, count));
last = s[i];
count = 1;
} else {
++count;
}
}
if (count > 0) {
a.push_back(make_pair(last, count));
}
int n = a.size();
for (; n > 1; --n) {
int i = n / 2 - 1;
int j = i + 1 + (n % 2);
for (; i >= 0; --i, ++j) {
if (a[i].first != a[j].first) break;
if (i > 0) {
if (a[i].second != a[j].second) break;
} else {
if (a[i].second > a[j].second) break;
}
}
if (i < 0) break;
}
string t;
for (int i = n; i < a.size(); ++i) {
t = string(a[i].second, a[i].first) + t;
}
if (n > 0 && a[0].second < a[n - 1].second) {
t += string(a[n - 1].second - a[0].second, a[0].first);
}
return t + s;
}
};
int main()
{
Solution s;
cout << s.shortestPalindrome("aacecaaa") << endl; // answer: aaacecaaa
cout << s.shortestPalindrome("abcd") << endl; // answer: dcbabcd
cout << s.shortestPalindrome("abbacd") << endl; // answer: dcabbacd
cout << s.shortestPalindrome(string(20000, 'a') + "cd" + string(20000, 'a')) << endl;
cout << s.shortestPalindrome(string(200000, 'a') + "cd" + string(200000, 'a')) << endl;
return 0;
}