Approach for https://mirror.codeforces.com/problemset/problem/1278/A (Sliding window using DP)

Revision en1, by Naithani, 2019-12-23 21:15:48

I have used an efficient way to insert into the set from the rear end and delete it from another(like sliding window). DP array will store the addresses of the inserted node, when we need to delete that node, we don't need to apply to find() function. Just use that address then the node is deleted... I hope this will be helpful for somebody...

#include<bits/stdc++.h>
#define endl '\n'
#define F(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std;

void solve()
{
    int t;
    cin >> t;

    while(t--)
    {
        string s;
        cin >> s;

        multiset <char> store;

        for(auto i: s) store.insert(i);

        string hash;
        cin >> hash;

        multiset <char> match;
        multiset<char>::iterator dp[hash.size()];

        bool possible = false;

        F(i,0, hash.size() -1)
        {
            dp[i] = match.insert(hash[i]);
             

            if(match.size()==store.size())
            {
                if(match == store)
                {
                    possible = true;
                    break;
                }
                else
                {
                    match.erase(dp[i-store.size()+1]);
                }

            }
        }

        if(possible) cout << "YES";
        else cout << "NO";

        cout << endl;

    }

}


int main(){
    
    solve();

    return 0;
}


Tags #dp, #set, sliding window

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Naithani 2019-12-23 21:15:48 1536 Hello guys, I just post my an approach for the above problem using sliding window with DP. I hope you like it. (published)