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

 

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) // record the last position of each char in string.
        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;
}

 

转载于:https://www.cnblogs.com/HisonSanDiego/p/8319787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值