• Substring with Concatenation of All Words


    You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.

    For example, given:
    S: "barfoothefoobarman"
    L: ["foo", "bar"]

    You should return the indices: [0,9].
    (order does not matter).

    假设L中的单位长度为n,依次从S中取长度为n的子串,如果在L中,就记下来。如果在L中,但是已经匹配过了,说明匹配重复,也需要从S的下一个位置开始重新匹配,因为匹配要是连续的,中间不允许插入其他不匹配或者重复匹配的单词。需要借助hash或map,如果整个L都匹配完了,就算是一个concatenation;当匹配错误的时候,S右移一个位置。

    C++实现代码:

    #include<iostream>
    #include<vector>
    #include<string>
    #include<map>
    using namespace std;
    
    class Solution {
    public:
        vector<int> findSubstring(string S, vector<string> &L) {
            vector<int> ret;
            int n=L[0].size();
            if(S.length()<n*L.size())
                return ret;
            map<string,int> mp;//记录L中每一个string出现的次数
            map<string,int> word; //记录在S中找到的L中string的次数,如果大于mp中的,说明找到重复的
            size_t i;
            for(i=0;i<L.size();i++)
                mp[L[i]]++;
            size_t idx=0;
            //idx之后至少还剩下L中所有字符串的长度
            while(idx<=S.length()-n*L.size())
            {
                word.clear();
                int flag=true;
                //i之后至少还剩下一个字符串的长度n,要取n长度的子串
                for(i=idx;i<=idx+n*(L.size()-1);i+=n)
                {
                    string tmp=S.substr(i,n);
                    if(mp.find(tmp)==mp.end())
                    {
                        flag=false;
                        break;
                    }
                    word[tmp]++;
                    if(word[tmp]>mp[tmp])
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                    ret.push_back(idx);
                idx++;
            }
            return ret;
        }
    };
    
    int main()
    {
        Solution s;
        vector<string> L={"foo", "bar"};
        vector<int> result=s.findSubstring(string("barfoothefoobarman"),L);
        for(auto a:result)
            cout<<a<<" ";
        cout<<endl;
    }
  • 相关阅读:
    git 存储stash 唏嘘
    Postman请求接口无响应解决案例(Could not send request) 唏嘘
    mybatis if判断字符串 唏嘘
    Mybatis if判断Integer类型的值 唏嘘
    mysql 按照中文首字母排序 唏嘘
    git add命令 唏嘘
    git stash drop stash@{0} error: unknown switch `e‘(window10) 唏嘘
    【原】3D游戏中的算法MMO游戏全视角3D相机,全地形中视觉障碍物自动缩进调整
    work diary
    高中物理错题本
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4136658.html
Copyright © 2020-2023  润新知