Sliding window [s2] is determined to contain full array s1

The general idea:

s2 s1 must contain some arrangement must contain the kind of continuous.

First I s1 output various arrangements with full array, where the recursive critical condition comprises determining whether s2. Obviously, even if pruning is still no response, indeed, to think, people like dfs recursion is used to output a combination of detailed information, but there did not seem necessary to do this. However, this code is still a place worth learning: ① recursive algorithm review the full array of output (the current character at or after the combination is not in , if you need to traverse in position); ② determine whether two strings containing function: String. == string.npos whether the Find . Do not "determine whether there is find the vector" and "to determine whether to include an array of characters strstr" and confused before.

#include<bits/stdc++.h>
class Solution {
public:
    bool flag = false;
    bool checkInclusion(string s1, string s2) {
        int len1=s1.size();
        int len2=s2.size();
        if(len1>len2)
            return false;
        if(len1==len2&&len1==0)
            return true;
        QuanPaiLie(s1,s2,0);
        if(flag)
            return true;
        else
            return false;
    }
    
    void QuanPaiLie(string &s,string s2,int begin)
    {
        if(flag==true) //剪枝
            return;
        
        if(begin==s.size()-1)
        {
            //判断是否被s2包含
            // if(find(s2.begin(),s2.end(),s)!=s2.end()) //find是用于在vector中找的!这里是字符串,不行
            // if( strstr(s2,s)!=NULL ) //字符串判断是否包含用str(母串,子串) 包含返回首地址,不包含则返回NULL。但是!!!前提是s2和s都是字符数组,而非字符串string类型
            //最后终于找到方法:string.find,没找到则返回string.npos
            if(s2.find(s)!=s2.npos)
            {
                flag=true;
                return;
            }
            else
                return;
        }
        for(int i=begin;i<s.size();i++)
        {
            swap(s,begin,i);
            QuanPaiLie(s,s2,begin+1);
            swap(s,begin,i);
        }
    }
    
    void swap(string &s,int i,int j)
    {
        int temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
    
};

 

Later, with the title analysis, saying it was perfection on the arrangement corresponds, in fact, to see it together is not the number of times individual letters appear are the same . (Regardless of how the arrangement, the number of times each letter appears is unchanged)

Then (after the understanding was that simple):

① two times each vector stored letters appear in the sliding window or in s1 and s2, v1 == v2 if it indicates the sliding window is the desired, returns true. Note that use vector, can be compared directly by == equal.

② mobile number of occurrences "sliding window", the first character -1, +1 the number of occurrences of a new character to the next, thus ensuring constant sliding window size translates rearward. Every translation should be judged once v1 == v2.

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        vector<int> v1(26,0); //记录s1各个单词出现的次数
        vector<int> v2(26,0); 
        int len1 = s1.size();
        int len2 = s2.size();
        if(len1>len2)
            return false;
        if(len1==len2&&len1==0)
            return true;
        
        for(int i=0;i<len1;i++) //先来看第一个滑动窗口
        {
            v1[s1[i]-'a']++;
            v2[s2[i]-'a']++;
        }
        if(v1==v2)
            return true; //表示s2的前len1个即s1的一种排列
        
        for(int i=0;i+len1<len2;i++) //长度为len1的滑动窗口开始向后平移
        {
            v2[s2[i]-'a']--; //后移头
            v2[s2[i+len1]-'a']++; //后移尾
        
            //每次后移一步都要看看匹配了吗
            if(v1==v2)
                return true;
        }
        return false;
    }
};

 

Guess you like

Origin blog.csdn.net/m0_38033475/article/details/92379743