56D - Changing a String - CodeForces Solution


dp *2100

Please click on ads to support us..

C++ Code:

//https://codeforces.com/contest/56/problem/D
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define PI acos(-1)
#define LSB(i) ((i) & -(i))
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define sc second
#define th third
#define fo fourth
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
#define INF 1e15
#define MOD 1000000007
#define endl "\n"

#define all(data)       data.begin(),data.end()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()
#define MAXN 1007
ll dp[MAXN][MAXN];
char s[MAXN],t[MAXN];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    string a,b; cin>>a>>b;
    ll n=a.size(),m=b.size();
    for(int i=1;i<=n;i++) s[i]=a[i-1];
    for(int i=1;i<=m;i++) t[i]=b[i-1];
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=m;j++) dp[i][j]=INF;
    }
    for(int i=0;i<=n;i++) dp[i][0]=i;
    for(int j=0;j<=m;j++) dp[0][j]=j;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++) dp[i][j]=min({dp[i][j],dp[i-1][j-1]+(ll)(s[i]!=t[j]),dp[i-1][j]+1,dp[i][j-1]+1});
    }
    cout<<dp[n][m]<<endl;
    ll i=n,j=m;
    while(i || j)
    {
        if(i && j && dp[i][j]==dp[i-1][j-1]+(ll)(s[i]!=t[j]) && s[i]!=t[j]) {cout<<"REPLACE "<<i<<" "<<t[j]<<endl; i--; j--;}
        if(i && j && dp[i][j]==dp[i-1][j-1]+(ll)(s[i]!=t[j]) && s[i]==t[j]) {i--; j--;}
        else if(i && dp[i][j]==dp[i-1][j]+1) {cout<<"DELETE "<<i<<endl; i--;}
        else if(j && dp[i][j]==dp[i][j-1]+1) {cout<<"INSERT "<<i+1<<" "<<t[j]<<endl; j--;}
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter