1492C - Maximum width - CodeForces Solution


binary search data structures dp greedy two pointers *1500

Please click on ads to support us..

Python Code:

from functools import lru_cache
from collections import defaultdict

def solution():
    n, m = map(int, input().split())
    s = input()
    t = input()

    leftMost = [None]*len(t)
    j = 0
    for i in range(len(s)):
        if j < len(t) and s[i] == t[j]:
            leftMost[j] = (i)
            j += 1

    rightMost = [None]*len(t)
    j = len(t)-1
    for i in range(len(s))[::-1]:
        if j >= 0 and s[i] == t[j]:
            rightMost[j] = i
            j -= 1

    mx = 0
    for i in range(1, len(leftMost)):
        ans = rightMost[i] - leftMost[i-1]
        mx = max(mx, ans)
    print(mx)
        
    

solution()

C++ Code:

/* {وَقُلْ رَبِّ زِدْنِي عِلْمًاً} */
#include<bits/stdc++.h>
using namespace std;
#define TY cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define ll long long int
#define f(nn) for(ll i = 0; i < (nn); i++)
#define frev(nn) for (ll i = nn - 1; i >= 0; --i)
void jo() {
    ll n,m;cin>>n>>m;
    string s,t;cin>>s>>t;
    vector<int>pref(n+1),suf(n+1);
    int j = 0;
    for (int i = 0; i < n; ++i) {
        if(s[i]==t[j]){
            pref[j] = i,j++;
            if(j==m)break;
        }
    }
    j--;
    frev(n){
        if(s[i]==t[j]){
            suf[j] = i,j--;
            if(j==-1)break;
        }
    }
    int ans = 0;
    f(m)
        ans=max(suf[i+1]-pref[i],ans);
    cout<<ans<<"\n";
}
int main() {
    TY
    jo();
}


Comments

Submit
0 Comments
More Questions

1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers