【codewars-12】Parts of a list. 分割list

Write a function partlist that gives all the ways to divide a list (an array) of at least two elements into two non-empty parts.

Each two non empty parts will be in a pair (or an array for languages without tuples or a structin C - C: see Examples test Cases - )
Each part will be in a string
Elements of a pair must be in the same order as in the original array.
Examples of returns in different languages:

a = ["az", "toto", "picaro", "zone", "kiwi"] 
--> [["az", "toto picaro zone kiwi"], ["az toto", "picaro zone kiwi"], ["az toto picaro", "zone kiwi"], ["az toto picaro zone", "kiwi"]] 

分析

最好是在对string列表进行一遍扫描后完成的pair的构造.
观察下表能发现以下规律:
第K个字符串出现在第K ~ 最后一个 pair的first中.
并且也出现在第0个~ K-1个pair的second中.

pair.firstpair.second
“az”“toto picaro zone kiwi”
“az toto”“picaro zone kiwi”
“az toto picaro”“zone kiwi”
“az toto picaro zone”“kiwi”
class PartList
{
public:
    static std::vector<std::pair <std::string, std::string>> partlist(std::vector<std::string> &arr){
      std::vector<std::pair<std::string, std::string>> res(arr.size()-1);
      
      for(size_t i =0 ;i< arr.size() ; ++i){
        // 组装 每个pair的首个元素
        for(size_t j = i ;j< res.size() ; ++j){
          std::string&  current = res[j].first ;
          if(current != "") current.append(" ");
          current.append(arr[i]);
        }
        // 组装 pair的第二个元素
        for(size_t k = 0 ; k< i ;++k){
          std::string&  current = res[k].second ;
          if(current != "") current.append(" ");
          current.append(arr[i]);
        }
        
        
      }
      return res;
    }
};

更好的解法

其实有更加直观的思路. 那就是每个pair的first和second分别是有字符串的前N个/后N个字符串拼接合成的一个长字符串

但由于对C++算法库不熟, 没想到能完成"多个合成为一个"这样的函数,
std::accumulate就能完成这样的任务. 其参数为:
( 开始位置, 结束位置, 初始值, 将每两个元素合成为一个的方法 )

#include <numeric>

using namespace std;

class PartList
{
public:
    static std::vector<std::pair <std::string, std::string>> partlist(std::vector<std::string> &arr)
    {
      vector<pair<string, string>> result;
      auto combine = [](string a, string b) { return a + ' ' + b; };
      for (int i = 1; i < arr.size(); i++)
      {
        result.emplace_back(
          accumulate(arr.begin() + 1, arr.begin() + i, arr[0], combine),
          accumulate(arr.begin() + i + 1,  arr.end(), arr[i], combine));
      }
      return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值