1553D - Backspace - CodeForces Solution


dp greedy strings two pointers *1500

Please click on ads to support us..

Python Code:

q = int(input())
for _ in range(q):
    s = input()
    t = input()
    pointer_s, pointer_t = len(s)-1, len(t)-1
    same = 0
    while pointer_s >= 0 and pointer_t >= 0:
        if s[pointer_s] == t[pointer_t]:
            same += 1
            pointer_s -= 1
            pointer_t -= 1
        else:
            pointer_s -= 2
        if same == len(t):
            break
    if same == len(t):
        print("YES")
    else:
        print("NO")
		 		 					   	    		         	

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ll long long
#define ld long double
#define L "\n"
#define F first
#define S second
#define pb push_back

const int N=6e5+50;

ll n,m,x,y,z,k,a,b,d,cnt,cnt1,cnt2,l,r,sum,sum1,sum2,ans;
map<pair<char,ll>,vector<ll>>mp;
int main()
{
    IOS
    ll T = 1;
    cin >> T;
    while( T-- )
    {
        bool p=false;
        mp.clear();
        string s,ss;
        cin>>s>>ss;
        ll n=s.size(),m=ss.size();
        for(ll i=0 ; i<n ; i++)
        {
            mp[{s[i],i%2}].pb(i);
        }
        ll i=0,x=0;
        for( ; i<m ; i++)
        {
            auto it=lower_bound(mp[{ss[i],p}].begin(),mp[{ss[i],p}].end(),x);
            if(it==mp[{ss[i],p}].end())
                break;
            x=*it;
            p=!p;
        }
        if(i==m and (n-x)%2) {cout<<"YES\n";continue;}
        p=true;
        i=0;x=0;
        for( ; i<m ;  i++)
        {
            auto it=lower_bound(mp[{ss[i],p}].begin(),mp[{ss[i],p}].end(),x);
            if(it==mp[{ss[i],p}].end())
                break;
            x=*it;
            p=!p;
        }
        if(i==m and (n-x)%2) {cout<<"YES\n";continue;}
        no;
    }
}


Comments

Submit
0 Comments
More Questions

1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference