2018-2 - target tags

这是亚麻2018 年的新题2

// find all the N substring with only one duplicate character.
#include <iostream>     // std::cout
#include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector>       // std::vector
#include <unordered_map>
#include <numeric>

//use a slide window to mared each matched range and record the optimal result.
//use the hash map to index the each substring in target.
//iterate all substring in available container and find out each slide window that is marked with the updated indexs stored in a vector container.

using namespace std;

std::vector<int> subStringKDistance(std::vector<std::string>& available, std::vector<std::string>& target){

    if (available.size() < target.size() || available.size ()== 0 || target.size() ==0 ) return  std::vector<int>();

    std::unordered_map<std::string, int> hashmap;

    // map substring in target to index.
    int i = 0;
    for (auto e: target){
        hashmap[e] = i++;
    }

    if (hashmap.size() != target.size()) return std::vector<int>();

    for (auto& e: hashmap)
        std::cout << e.first << " " << e.second;

    //create container to store the indexs of each substring in available list.
    std::vector<int > indexs(hashmap.size(), INT_MAX);

    int ind1 = 0;
    int ind2 =0;
    int distance = INT_MAX;

    int index = 0;
    for (auto e: available){

        if ( hashmap.find(e) == hashmap.end()) {// the string is not in target list.
            index++;
            std::cout<< e << " not found"<<std::endl;
            continue ;  //use find to check if the elelement is a target tag.
        }

        // the substring is in target list and update its index.
        indexs[hashmap[e]] = index;

        std::cout << e << "is found "<< " index : "<< index  << std::endl;

        int max =0;
        int min =0;
        max = *std::max_element(indexs.begin(), indexs.end());
        if ( max != INT_MAX){// all the substring in target have been found at less one time. so now can check the distance.
            min = *std::min_element(indexs.begin(),indexs.end());

            std::cout<<"max : "<< max << " min: "<< min << std::endl;

            //find a more small distance. update the index and distance.
            if ( distance > (max - min)){
                distance  = max - min;
                ind1 = min ;
                ind2 = max;
            }
        }
        index++;
    }

    if ( distance == 0)
        return {0};
    else
        return {ind1, ind2};
}

int main () {
//    std::vector<std::string> ta = {"made", "in" , "spain"};
//    std::vector<std::string> av = {"made", "weather","forecast","says","taht","made","rain","in","spain","stay"};

//    std::vector<std::string> ta = {"2abc", "bcd" , "cab"};
//    std::vector<std::string> av = {"dbc", "2abc","cab","bcd","bcb"};
//
    std::vector<std::string> ta = {"in", "the" , "spain"};
    std::vector<std::string> av = {"the", "spain","that","the","rain","in","spain","stays","forcaste","in"};

    auto res = subStringKDistance(av, ta);

    for (auto e: res)
        std::cout<< e << " ";

    return 0;
}

原文地址:https://www.cnblogs.com/HisonSanDiego/p/8283283.html

时间: 2024-08-30 13:07:32

2018-2 - target tags的相关文章

base库插件---拖动

1 /** 2 * Created by Administrator on 2014/6/5 0005. Base-drag 基于Base库的拖拽插件 tags为你要拖拽的元素参数, 数组形式传入 3 */ 4 5 $().extend('drag', function () { 6 var tags = arguments; 7 for (var i = 0; i < this.elements.length; i ++) { 8 addEvent(this.elements[i], 'mou

103 extend 插件入口 jquery eq 和get的封装 拖拽插件

//demo.js window.onload = function () { //个人中心 $().getClass("member").hover(function(){ $(this).css("background","url(images/arrow2.png) no-repeat 55px center"); $().getClass("member_ul").show(); },function(){ $().g

用js完成blog项目

1 //前台调用 2 var $ = function (args) { 3 return new Base(args); 4 } 5 6 //基础库 7 function Base(args) { 8 //创建一个数组,来保存获取的节点和节点数组 9 this.elements = []; 10 11 if (typeof args == 'string') { 12 //css模拟 13 if (args.indexOf(' ') != -1) { 14 var elements = arg

windows vs code 配置调试php

1.安装好vs code 2.安装好nginx 3.下载好php执行文件 php.ini [PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; PHP's initialization file, generally called php.ini, is responsible for ; configuring many of the aspects of PHP's behavior. ; PHP attempts

wampserver2.2e-php5.3.13 版本 增加 php7 支持

公司需要升级PHP版本至PHP7,对于用惯了wamp的来说,添加一下PHP扩展应该是很容易的,看我以前的文章(WampServer自己DIY添加apache.php.mysql版本). 不过再配置的当中并不顺利,要集成php7.1.15花了一点时间,因为使用的是 wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-32b.exe 这个版本,导致集成出现两个问题: 1.php7版本提供的apache扩展dll文件并不支持apache2.2,只支持apa

webbench安装和简单使用

一.安装流程 wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd webbech-1.5 make make install常见问题: (1)错误信息一: /bin/sh: 1: ctags: not found Makefile:12: recipe for target 'tags' failed 解决办法:sudo apt-get instal

如何将C/C++程序转译成Delphi(十四)

众所周知,数据科学是这几年才火起来的概念,而应运而生的数据科学家(data scientist)明显缺乏清晰的录取标准和工作内容.此次课程以<星际争霸II>回放文件分析为例,集中在IBM Cloud相关数据分析服务的应用.面对星际游戏爱好者希望提升技能的要求,我们使用IBM Data Science Experience中的jJupyter Notebooks来实现数据的可视化以及对数据进行深度分析,并最终存储到IBM Cloudant中.这是个介绍+动手实践的教程,参会者不仅将和讲师一起在线

世界著名设计小组nrg推荐的75个FLASH酷站

众所周知,数据科学是这几年才火起来的概念,而应运而生的数据科学家(data scientist)明显缺乏清晰的录取标准和工作内容.此次课程以<星际争霸II>回放文件分析为例,集中在IBM Cloud相关数据分析服务的应用.面对星际游戏爱好者希望提升技能的要求,我们使用IBM Data Science Experience中的jJupyter Notebooks来实现数据的可视化以及对数据进行深度分析,并最终存储到IBM Cloudant中.这是个介绍+动手实践的教程,参会者不仅将和讲师一起在线

铺揭竟冒剐惹蒲掷咕堑接veld

IEEE Spectrum 杂志发布了一年一度的编程语言排行榜,这也是他们发布的第四届编程语言 Top 榜. 据介绍,IEEE Spectrum 的排序是来自 10 个重要线上数据源的综合,例如 Stack Overflow.Twitter.Reddit.IEEE Xplore.GitHub.CareerBuilder 等,对 48 种语言进行排行. 与其他排行榜不同的是,IEEE Spectrum 可以让读者自己选择参数组合时的权重,得到不同的排序结果.考虑到典型的 Spectrum 读者需求