码迷,mamicode.com
首页 > 其他好文 > 详细

2018-3 video sequence ( find the list of size of the each sub sequence )

时间:2018-01-20 11:07:32      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:string   his   amp   reverse   order   scene   ios   bcd   nbsp   

 

2018 亚麻新题

 

http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=306458&extra=page%3D12%26filter%3Dsortid%26sortid%3D311%26sortid%3D311

 

第二题是个新题也是一个string. string里每个character代表一个movie shot, 要把这些character做partition组成一个movie scene, 要求每个partition里的character 只能在这个partition里出现。比如说
"abacdfeffhijkik"
我们可以分成三份 "abacd | feffh | ijkik"
输出是每个partition的size组成的list.

 

// reverse algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::reverse
#include <vector>       // std::vector
#include <unordered_map>


//key point
// the complexity is (n)
//use the unordered_map< char, last position of this char> to record the last position of each char in string.
//use the slide window to iterate each char in string .
    // <sp , ep> is the slide window. use ep to indicator the last postion of the current substring that is substring< sp, cur >.
    // continue move ‘cur‘ in window, if last postion of char in ‘cur‘ position is large than ep, update the ep to that postion. then the size of the window is changed. if ‘cur‘ ==‘ep‘, the a substring found that meet the requirement of the problem.

using namespace std;

vector<int > findSequenceSize(string& s ){
    if(s.empty()) return {0};
    
    unordered_map <char , int > charPos;
    vector<int > res;
    
    int index =0;
    for ( auto& e : s)
        charPos[e] = index++;
    
    int sp =0; int ep =0; int cur =0;
    
    while ( ep < s.size ()){
        
        ep = charPos[s[ep]]; // find the last index of this char.
        //cout <<" the ep is " << ep<< endl;
        
        if ( ep != sp)
            cur = sp+1;  // more than one char in window.
        else
            goto CONTINUE ;  //only one char taht meet the requirement(in window).
        
        while ( cur != ep){
            int i = charPos[s[cur]];
            if(i > ep){  //adjust the size of the window.
                ep = i;
            }
            cur ++;  // move to the next char in window
        }
        
    CONTINUE :  // find one substring meet the requirment.
        res.push_back(ep - sp +1);
        cout << " the substring is " << s.substr(sp, ep-sp+1)<< endl;
        ep++;
        sp = ep;
        cur =sp;
            
    }
    return res;
    
}

int main () {
    string in = {"abacdfeffhijkik"};
    //string in = {"abcdd"};
    
    auto&& out = findSequenceSize ( in );
    
    for ( auto& e : out )
        cout <<e << " ";
    
    

    return 0;
}

 

2018-3 video sequence ( find the list of size of the each sub sequence )

标签:string   his   amp   reverse   order   scene   ios   bcd   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!