# 555. Split Concatenated Strings 分割连接字符串

@TOC

## # 题目描述

Given a list of strings, you could concatenate these strings together into a loop, where for each string you could choose to reverse it or not. Among all the possible loops, you need to find the lexicographically biggest string after cutting the loop, which will make the looped string into a regular one.

Specifically, to find the lexicographically biggest string, you need to experience two phases:

Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given. Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from the character at the cutpoint. And your job is to find the lexicographically biggest one among all the possible regular strings.

Example:

``````Input: "abc", "xyz"
Output: "zyxcba"
Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-",
where '-' represents the looped status.
The answer string came from the fourth looped one,
where you could cut from the middle character 'a' and get "zyxcba".
``````

Note:

1. The input strings will only contain lowercase letters.
2. The total length of all the strings will not over 1,000.

## # 解题方法

### # 遍历

C++代码如下：

``````class Solution {
public:
string splitLoopedString(vector<string>& strs) {
const int N = strs.size();
vector<string> reversed;
for (string& str : strs) {
string rever = str;
reverse(rever.begin(), rever.end());
reversed.push_back(rever > str ? rever : str);
}
string res;
for (int pos = 0; pos < N; ++pos) {
string& str = reversed[pos];
string rever = str;
reverse(rever.begin(), rever.end());
for (string cur : {str, rever}) {
for (int i = 0; i < cur.size(); ++i) {
string curres;
curres += cur.substr(i);
for (int j = pos + 1; j < N; ++j) {
curres += reversed[j];
}
for (int j = 0; j < pos; ++j) {
curres += reversed[j];
}
curres += cur.substr(0, i);
res = res > curres ? res : curres;
}

}

}
return res;
}
};
``````

## # 日期

2019 年 9 月 19 日 —— 举杯邀明月，对影成三人