Which are in?

来源:互联网 发布:知乎和知网 编辑:程序博客网 时间:2024/06/06 20:40

Which are in?

Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.

Example 1: a1 = [“arp”, “live”, “strong”]
a2 = [“lively”, “alive”, “harp”, “sharp”, “armstrong”]
returns [“arp”, “live”, “strong”]

Example 2: a1 = [“tarp”, “mice”, “bull”]

a2 = [“lively”, “alive”, “harp”, “sharp”, “armstrong”]>

returns []

Notes: Arrays are written in “general” notation. See “Your Test Cases” for examples in your language.

Beware: r must be without duplicates.

#include <vector>#include <algorithm>#include <iterator>#include <iostream>class WhichAreIn{public:    static std::vector<std::string> inArray(std::vector<std::string> &array1, std::vector<std::string> &array2)    {        std::vector<std::string> result;        std::copy_if(array1.begin(), array1.end(), std::back_inserter(result), [=](std::string str) {            std::vector<std::string>::const_iterator it = array2.end();            return std::any_of(array2.begin(), array2.end(), [=](std::string str2) {                return str2.find(str) != std::string::npos;            });        });        std::sort(result.begin(), result.end(), [](std::string a, std::string b) {            return a < b;        });        return result;    }};int main(){    std::vector<std::string> arr1 = { "live","arp", "strong" };    std::vector<std::string> arr2 = { "lively", "alive", "harp", "sharp", "armstrong" };    std::vector<std::string> sol1 = { "arp", "live", "strong" };    std::vector<std::string> ans1 = WhichAreIn::inArray(arr1, arr2);    return 0;}
阅读全文
0 0
原创粉丝点击