Untitled

mail@pastecode.io avatar
unknown
plain_text
3 days ago
1.8 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;


bool isSunsequence(string &s1, string &s2){
    int n = s1.size();
    int m = s2.size();
    
    int i=0;
    int j =0;
    
    while(i<n){
        if(s1[i] == s2[j]){
            j++;
        }
        i++;
    }
    
    return j == m;
}


bool isPossible(string &s1, string &s2, vector<pair<int, int>> &mutations, int mid){
    vector<int> mutationEffect(s1.size()+1, 0);
    
    for(int i=0; i<mid; i++){
        int st = mutations[i].first;
        int e = mutations[i].second;
        mutationEffect[st]++;
        if(e+1 < s1.size()+1){
            mutationEffect[e+1]--;
        }
        
    }
    for(int i=1; i<s1.size(); i++){
        mutationEffect[i] += mutationEffect[i-1];
    }
    
    for(int i=0; i<s1.size(); i++){
        if(mutationEffect[i] > 0){
            s1[i] = '$';
        }
    }
    
    return isSunsequence(s1, s2);
}


int maxDaysToMutate(string &s1, string &s2, vector<pair<int, int>> &mutations){
    int l =0;
    int r = mutations.size();
    int ans = 0;
    while(l<=r){
        int mid = (l+r)/2;
        if(isPossible(s1, s2, mutations, mid)){
            ans = mid;
            l = mid +1;
        }
        else{
            r = mid-1;
        }
    }
    return ans;
}


int solve(string &s1, string &s2, vector<int> &start, vector<int> &end){
    int n = start.size();
    
    vector<pair<int, int>> mutations;
    for(int i=0; i<n; i++){
        mutations.push_back({start[i], end[i]});
    }
    
    return maxDaysToMutate(s1, s2, mutations);
}


int main() {
	// your code goes here
	string s1, s2;
	cin>>s1>>s2;
	
	int n;
	cin>>n;
	
	vector<int> start(n);
	vector<int> end(n);
	
	for(int i=0; i<n; i++){
	    cin>>start[i];
	}
	
	for(int i=0; i<n; i++){
	    cin>>end[i];
	}
	
	int ans = solve(s1, s2, start, end);
	cout<<ans<<endl;

}
Leave a Comment