​LeetCode刷题实战555:分割连接字符串

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 分割连接字符串,我们先来看题面:

https://leetcode-cn.com/problems/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.

给定一个字符串列表,你可以将这些字符串连接成一个循环字符串,对于每个字符串,你可以选择是否翻转它。在所有可能的循环字符串中,你需要分割循环字符串(这将使循环字符串变成一个常规的字符串),然后找到字典序最大的字符串。

具体来说,要找到字典序最大的字符串,你需要经历两个阶段:

将所有字符串连接成一个循环字符串,你可以选择是否翻转某些字符串,并按照给定的顺序连接它们。

在循环字符串的某个位置分割它,这将使循环字符串从分割点变成一个常规的字符串。

你的工作是在所有可能的常规字符串中找到字典序最大的一个。

示例                         

输入: “abc”, “xyz”
输出: “zyxcba”
解释: 你可以得到循环字符串 “-abcxyz-”, “-abczyx-”, “-cbaxyz-”, “-cbazyx-”,
其中 ‘-’ 代表循环状态。
答案字符串来自第四个循环字符串,
你可以从中间字符 ‘a’ 分割开然后得到 “zyxcba”。

注意:
输入字符串只包含小写字母。
所有字符串的总长度不会超过 1,000。

解题

https://cloud.tencent.com/developer/article/1787950?ivk_sa=1024320u

首先求得每个字符串反转后是否比原来大,保留大的

然后考虑从哪个字符串切开(还要考虑该字符串逆序的情况,因为要切开,不知道字典序大小),从其哪个位置切开 .

b95615933dea3b05af70f34e2b363135.png

class Solution {
public:
    string splitLoopedString(vector<string>& strs) {
      string rev, all;
      for(string& s : strs)
      {
        rev = s;
        reverse(rev.begin(), rev.end());
        if(rev > s)//反转后的更大
          s = rev;
      }
      string ans, temp;
      for(int i = 0, j, k; i < strs.size(); ++i)
      { //从字符串 i 切开
            temp = "";
        for(j = i+1; j < strs.size(); ++j)
          temp += strs[j];//前部分是后面的 i+1, n-1
        for(j = 0; j < i; ++j)
          temp += strs[j];//接着是前面的 0, i-1
        for(j = 0; j <= strs[i].size(); ++j)
        {
          rev = strs[i].substr(j)+temp+strs[i].substr(0,j);
          ans = max(ans, rev);
        }
            reverse(strs[i].begin(), strs[i].end());//还要考虑反转后的情况
        for(j = 0; j <= strs[i].size(); ++j)
        {
          rev = strs[i].substr(j)+temp+strs[i].substr(0,j);
          ans = max(ans, rev);
        }
            reverse(strs[i].begin(), strs[i].end());//反转回去
      }
      return ans;
    }
};

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-540题汇总,希望对你有点帮助!

LeetCode刷题实战541:反转字符串 II

LeetCode刷题实战542:01 矩阵

LeetCode刷题实战543:二叉树的直径

LeetCode刷题实战544:输出比赛匹配对

LeetCode刷题实战545:二叉树的边界

LeetCode刷题实战546:移除盒子

LeetCode刷题实战547:省份数量

LeetCode刷题实战548:将数组分割成和相等的子数组

LeetCode刷题实战549:二叉树中最长的连续序列

LeetCode刷题实战550:游戏玩法分析 IV

LeetCode刷题实战551:学生出勤记录 I

LeetCode刷题实战552:学生出勤记录 II

LeetCode刷题实战553:最优除法

LeetCode刷题实战554:砖墙

ef2b710f87a3e310a8c5afd0dcefaaf3.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值