题目地址:https://leetcode-cn.com/problems/before-and-after-puzzle/

题目描述

Given a list of phrases, generate a list of Before and After puzzles.

A phrase is a string that consists of lowercase English letters and spaces only. No space appears in the start or the end of a phrase. There are no consecutive spaces in a phrase.

Before and After puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase.

Return the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] where i != j. Note that the order of matching two phrases matters, we want to consider both orders.

You should return a list of distinct strings sorted lexicographically.

Example 1:

Input: phrases = ["writing code","code rocks"]
Output: ["writing code rocks"]

Example 2:

Input: phrases = ["mission statement",
"a quick bite to eat",
"a chip off the old block",
"chocolate bar",
"mission impossible",
"a man on a mission",
"block party",
"eat my words",
"bar of soap"]
Output: ["a chip off the old block party",
"a man on a mission impossible",
"a man on a mission statement",
"a quick bite to eat my words",
"chocolate bar of soap"]

Example 3:

Input: phrases = ["a","b","a"]
Output: ["a"]

Constraints:

  1. 1 <= phrases.length <= 100
  2. 1 <= phrases[i].length <= 100

题目大意

给你一个「短语」列表 phrases,请你帮忙按规则生成拼接后的「新短语」列表。
「短语」(phrase)是仅由小写英文字母和空格组成的字符串。「短语」的开头和结尾都不会出现空格,「短语」中的空格不会连续出现。
「前后拼接」(Before and After puzzles)是合并两个「短语」形成「新短语」的方法。我们规定拼接时,第一个短语的最后一个单词 和 第二个短语的第一个单词 必须相同。
返回每两个「短语」 phrases[i] 和 phrases[j](i != j)进行「前后拼接」得到的「新短语」。

解题方法

保存首尾字符串

假如两个不同短语的首尾单词是相等的,就可以拼接到一起。因此重点在找到每个短语的首尾单词是什么。由于C++的string不支持split,使用了遍历的方法,找到第一个空格和最后一个空格出现的位置,从而分割出首单词head和尾单词tail,放入一个字典中。字典的格式是{字符串的索引:{首单词,尾单词}}

之后我们对每个短语都进行遍历查找其余的短语的头单词是否等于当前短语的尾单词,若相等则可以进行拼接。注意拼接的时候,重叠的首尾单词只需要保留其中一个。

最后,题目要我们返回排序了的拼接后的新短语,在C++中可以直接使用set进行排序。

C++代码如下:

class Solution {
public:
vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
const int N = phrases.size();
unordered_map<int, pair<string, string>> m;
for (int i = 0; i < N; ++i) {
string& phrase = phrases[i];
int first = -1;
int last = -1;
for (int j = 0; j < phrase.size(); ++j) {
if (phrase[j] == ' ') {
if (first == -1) {
first = j;
}
last = j;
}
}
string head = phrase.substr(0, first);
string tail = phrase.substr(last + 1);
m[i] = make_pair(head, tail);
}
set<string> s;
for (int i = 0; i < N; ++i) {
string& cur = phrases[i];
for (auto& other : m) {
if (other.first == i)
continue;
if (other.second.first == m[i].second) {
s.insert(cur + phrases[other.first].substr(m[i].second.size()));
}
}
}
return vector<string>(s.begin(), s.end());
}
};

日期

2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?

最新文章

  1. python学习八皇后问题
  2. linux命令——rm
  3. C语言有关数组的几点
  4. 活动图activity diagram
  5. 网时|ipone8爆冷,我的服务器空欢喜一场
  6. 4.4、Android Studio在命令行运行Gradle
  7. Linux下*.tar.gz/.tar.bz2 文件解压缩安装命令
  8. linux下串口函数
  9. ajax相关问题
  10. Deep learning with Python 学习笔记(7)
  11. vue自学入门-3(vue第一个例子)
  12. debian设置软件源为阿里云
  13. Java中的IO流,Input和Output的用法,字节流和字符流的区别
  14. 【LCA】BZOJ1776-[Usaco2010 Hol]cowpol 奶牛政坛
  15. vs已停止工作
  16. python序列化数据
  17. centos下nginx安装与配置
  18. 两个栈实现队列 Python实现
  19. 某谷 P5159 WD与矩阵
  20. 一次delete速度异常慢的处理过程

热门文章

  1. Yii自定义全局异常,接管系统异常
  2. Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
  3. 基于 Helm 快速部署 Wordpress
  4. 生产调优3 HDFS-多目录配置
  5. idea安装插件 JClassLib Bytecode viewer
  6. HDFS【概述、数据流】
  7. myatoi
  8. STL全特化与偏特化
  9. adb命令对app进行测试
  10. redis 之 哨兵