[LeetCode] Split Concatenated Strings 分割串聯字符串


 

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:

  1. Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
  2. 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.

 

這道題給了我們一些字符串,讓我們將其連接起來,連接的時候對於每個字符串我們可以選擇翻轉或者不翻轉,在行程的大的字符串上找一個位置cut掉,將該位置當作首字符,前面的字符串移動到末尾去,問怎么cut能使字符串的字母順序大。剛開始博主想,既然要讓最終字符串字母順序最大,那么每一個字符串當然要盡可能的大了,所以如果其翻轉字符串的字母順序大的話,就要對字符串進行翻轉。然后在組成的字符串中找最大的字符進行cut,然而這種思路不一定能得到正確的結果。比如字符串數組["lc", "love", "ydc"],如果按照博主之前的思路得到的字符串應該為"ydclclove",但正確結果應該是"ylclovecd"。我們可出來正確的答案中cut位置所在的字符串ydc,雖然cdy小於ydc,但還是翻轉了。但是其他的字符都是按照字母順序來確定要不要翻轉的,那么我們可以得出這樣的結論,只有cut所在的字符串的翻轉可能不按規律。那么我們如何確定cut位置呢,其實沒有太好的辦法,只能遍歷每一個字母。我們首先來根據字母順序確定要不要翻轉每一個字符串,將字母順序大的連成一個字符串,然后遍歷每一個字符串,在每一個字符串中遍歷每一個位置,將當前遍歷的字符串后面的所有字符串跟前面所有字符串先連起來,存入mid中,然后取出當前遍歷的字符串中當前遍歷的位置及其后面的字符取出,連上mid,然后再連上當前位置前面的字符,然后跟結果res比較,取較大者存入結果res。這里我們可以進行小優化,如果cut位置的字母大於等於結果res的首字母,我們才進行對比更新。注意我們在遍歷每個字符串時,要將其翻轉字符串的每一位也遍歷了,這樣才能涵蓋所有的情況,參見代碼如下:

 

class Solution {
public:
    string splitLoopedString(vector<string>& strs) {
        if (strs.empty()) return "";
        string s = "", res = "a";
        int n = strs.size(), cur = 0;
        for (string str : strs) {
            string t = string(str.rbegin(), str.rend());
            s += str > t ? str : t;
        }
        for (int i = 0; i < n; ++i) {          
            string t1 = strs[i], t2 = string(t1.rbegin(), t1.rend());
            string mid = s.substr(cur + t1.size()) + s.substr(0, cur);
            for (int j = 0; j < strs[i].size(); ++j) {
                if (t1[j] >= res[0]) res = max(res, t1.substr(j) + mid + t1.substr(0, j));
                if (t2[j] >= res[0]) res = max(res, t2.substr(j) + mid + t2.substr(0, j));
            }
            cur += strs[i].size();
        }
        return res;
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/86545/c-9ms-12-lines/2

https://discuss.leetcode.com/topic/86509/easy-understanding-c-solution-with-detailed-explnation

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM