[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:
- 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:
- The input strings will only contain lowercase letters.
- 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 = ;
for (string str : strs) {
string t = string(str.rbegin(), str.rend());
s += str > t ? str : t;
}
for (int i = ; i < n; ++i) {
string t1 = strs[i], t2 = string(t1.rbegin(), t1.rend());
string mid = s.substr(cur + t1.size()) + s.substr(, cur);
for (int j = ; j < strs[i].size(); ++j) {
if (t1[j] >= res[]) res = max(res, t1.substr(j) + mid + t1.substr(, j));
if (t2[j] >= res[]) res = max(res, t2.substr(j) + mid + t2.substr(, 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 题目讲解汇总(持续更新中...)
[LeetCode] Split Concatenated Strings 分割串联字符串的更多相关文章
- [LeetCode] 555. Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- LeetCode Split Concatenated Strings
原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/ 题目: Given a list of st ...
- 【LeetCode】555. Split Concatenated Strings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- LeetCode简单算法之分割平衡字符串 #1221
在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- C语言实现split以某个字符分割一个字符串
方式一: 使用strtok # include <string.h> # include <stdio.h> void split(char *src,const char * ...
- split 将字符串分割成字符串数组
list_name = list_name.split(","); split() 方法用于把一个字符串分割成字符串数组. 语法 stringObject.split(separa ...
- java:字符串的split方法,使用多个分隔符,分割一个字符串
java语言中,多个分隔符,分割一个字符串: String[] tmpAuthors=tempAuthorStr.split(";|,|:|,"); 可以在线测试:java代码 在 ...
- [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
随机推荐
- Sublime Text、webstorm等编译器快速编写HTML/CSS代码的技巧
Sublime Text.webstorm等编译器,如果你从事Web前端开发的话,对这几款软件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演 ...
- Spring MVC的handlermapping之BeanNameUrlHandlerMapping初始化
先介绍一下: BeanNameUrlHandlerMapping是基于配置文件的方式; 所有处理器需要在XML文件中,以Bean的形式配置. 缺点:配置繁琐; 如果多个URL对应同一个处理器,那么需要 ...
- python多进程并发redis
Redis支持两种持久化方式RDB和AOF,RDB持久化能够快速的储存和回复数据,但在服务器停机时会丢失大量数据,AOF持久化能够高效的提高数据的安全性,但在储存和恢复数据方面要耗费大量的时间,最好的 ...
- Win调整和小技巧
推荐win下一些个人爱用的工具软件(以及使用心得)和一些系统调整方法,让win下不尽人意的设置发生小小变化,让整天摸着电脑的ITer们的生活更有乐趣. 本人酷爱收集一些好用的软件,若各位也对某个或某些 ...
- 【Nginx系列】Nginx虚拟主机的配置核日志管理
Nginx配置段 #user nobody; worker_processes 1;// 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数 #error_lo ...
- DEVC使用问题集锦
一.DEVC++编译出现"Id return 1 exit status" 这是初学者刚用DEVC经常碰到问题,一般有如下解决方法: 1.首先检查下是否有c的exe程序开着,若开着 ...
- fflush(stdin)与fflush(stdout)
1.fflush(stdin): 作用:清理标准输入流,把多余的未被保存的数据丢掉.. 如: int main() { int num; char str[10]; cin>>num; c ...
- Lucene 的索引文件锁原理
Lucene 的索引文件锁原理 2016/11/24 · IT技术 · lucene 环境 Lucene 6.0.0Java “1.8.0_111”OS Windows 7 Ultimate 线程 ...
- python 字符串和字典
一.字符串操作 name = "my name is \t {name} and i am {year} years old" 1.首字母大写 print(name.capital ...
- windows安装虚拟主机virtualbox遇到的困难
本来想到可以在windows安装虚拟主机virtualbox,但是怎么自己的windows是盗版的,由于主题已经被破解了,所以不能安装结果强制性的进入pe然后从网上下载的dll文件复制到 c/wind ...