forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
36 lines (36 loc) · 1 KB
/
Solution.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
class Solution {
public:
string splitLoopedString(vector<string>& strs) {
for (auto& s : strs) {
string t{s.rbegin(), s.rend()};
s = max(s, t);
}
int n = strs.size();
string ans = "";
for (int i = 0; i < strs.size(); ++i) {
auto& s = strs[i];
string t;
for (int j = i + 1; j < n; ++j) {
t += strs[j];
}
for (int j = 0; j < i; ++j) {
t += strs[j];
}
for (int j = 0; j < s.size(); ++j) {
auto a = s.substr(j);
auto b = s.substr(0, j);
auto cur = a + t + b;
if (ans < cur) {
ans = cur;
}
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
cur = b + t + a;
if (ans < cur) {
ans = cur;
}
}
}
return ans;
}
};