class Solution {
public:
string gethead(string str){//获取头单词
string ret = "";
int strlen = str.length();
for(int i = 0; i < strlen; ++i){
if(str[i] == ' '){
break;
}
ret += str[i];
}
return ret;
}
string gettail(string str){//获取尾单词
int strlen = str.length();
int index = 0;
for(int i = strlen - 1; i >= 0; i--){
if(str[i] == ' '){
index = i;
break;
}
}
if(index == 0){
return str;
}
return str.substr(index+1,strlen);;
}
string deletesubstr(string str,string substr){//删除重复的单词【为了拼接】;只能删除第一个单词,不能删除最后一个单词【与空格有关】
string strT = str;
int pos = strT.find(substr);
if (pos >-1)
{
strT.erase(pos,substr.size());
}
return strT;
}
vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
int phraseslen = phrases.size();
vector<string> head;
vector<string> tail;
for(int i = 0; i < phraseslen; ++i){//获取头尾词的数组
head.push_back(gethead(phrases[i]));
tail.push_back(gettail(phrases[i]));
}
vector<string> ret;
for(int j = 0; j < phraseslen; ++j){//尾部
for(int k = 0; k < phraseslen; ++k){
if(j != k && tail[j] == head[k]){//如果某个子串的头等于另一个子串的尾
ret.push_back(phrases[j] + deletesubstr(phrases[k],head[k]));//把他拼接起来放在返回容器中
cout << phrases[j] + deletesubstr(phrases[k],head[k]) << endl;
}
}
}
sort(ret.begin(), ret.end());
ret.erase(unique(ret.begin(), ret.end()), ret.end());//去重
return ret;
}
};

LEETCODE - 1181【前后拼接】的更多相关文章

  1. LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)

    这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...

  2. 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数

    最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...

  3. Leetcode 321.拼接最大数

    拼接最大数 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个 ...

  4. Java实现 LeetCode 321 拼接最大数

    321. 拼接最大数 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要 ...

  5. 【LeetCode】1181. Before and After Puzzle 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存首尾字符串 日期 题目地址:https://lee ...

  6. leetCode刷题(找到两个数组拼接后的中间数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. leetcode 321. 拼接最大数(单调栈,分治,贪心)

    题目链接 https://leetcode-cn.com/problems/create-maximum-number/ 思路: 心都写碎了.... 也许就是不适合吧.... 你是个好人... cla ...

  8. [LeetCode] Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  9. [LeetCode] Ternary Expression Parser 三元表达式解析器

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

随机推荐

  1. CTFHub - Web(五)

    eval执行: 1.进入网页,显示源码, <?php if (isset($_REQUEST['cmd'])) { eval($_REQUEST["cmd"]); } els ...

  2. ctfhub技能树—信息泄露—hg泄露

    打开靶机 查看页面信息 使用dvcs-ripper工具进行处理 ./rip-hg.pl -v -u http://challenge-cf630b528f6f25e2.sandbox.ctfhub.c ...

  3. 用 UniRx 实现 Timeline 式的异步操作

      没接触 UniRx 之前,我在 Unity 中通常用 Coroutine 或 Callback 来实现异步操作.根据我的任务,一般都是去实现游戏组件的演出,比如:敌方角色图形显示后,我方角色 UI ...

  4. Jmeter的Cookie管理器调试与参数化

     默认系统都是需要登录,才能操作其它接口,所以需要添加一个HTTP Cookie 管理器,默认Cookie管理器是关闭的,需要修改jmeter配置文件jmeter.properties,该文件在jme ...

  5. Bitter.Core系列二:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之数据库连接

    Bitter.Core NETCore 相当的简单易用,下面附上使用示例: 数据中连接:请在你的NETCORE 项目中 创建:Bitter.json 配置文件,然后追加如下配置内容: MSSQL 连接 ...

  6. Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740

    Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...

  7. SICP 解题集 — SICP 解题集 https://sicp.readthedocs.io/en/latest/

    SICP 解题集 - SICP 解题集 https://sicp.readthedocs.io/en/latest/

  8. __init__ raises an exception, then __del__ will still be called

    issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org ...

  9. 日记 + sb错误

    置顶消息cpdd 1.29 完了,文化课没了 我是废物 1.28 更新了自己的副标题 前副标题:Future never has to do with past time,but present ti ...

  10. bzoj2654(loj20069)

    2654: tree Time Limit: 30 Sec  Memory Limit: 512 MB Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有ne ...