given two string a and b of same length.
in one operation you can set a[x] equal to b[x] this cost 1 coin
you can also reverse a substring a[i…j] almost once, this cost
find min operation req. to make both string equal
0<=a=b<=1000
0<=c<=a
eg.
a=abceda
b=bdecbo
c=1
o/p–> 3
a=finger
b=ginger
c=0
o/p–>1
Why is the cost given if you only care about minimum operations?
Edit: Either way it can be done with dp
dp[i][j] = gain over the original string if you reverse A[i]…A[j]
You need only dp[i+1][j-1] to build dp[i][j], so n ^ 2.
Ans is min of ((max over all dp[i][j] values + changes after reversing A[i]…A[j]) ,Total changes without using reversal)
If you have better testcases, please.
hey can u share the code, I didn’t get what u did there
it was asked today in my campus placements. sed
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
const int N = 2e5 + 500;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
int c;
cin >> c;
int n = s.length();
vector<vector<int>> dp(n, vector<int> (n));
for(int len = 1;len<=n;len++) {
for(int i=0;i+len<n;i++) {
int j = i + len;
//reverse i ... j
dp[i][j] = dp[i+1][j-1] + (s[i] == t[j]) + (s[j] == t[i]) - (s[i]==t[i]) -(s[j]==t[j]);
}
}
int mxg = 0;
pair<int,int> ind = {0,0};
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
if(dp[i][j] > mxg) {
mxg = dp[i][j];
ind = {i,j};
}
}
}
//mxg is max gain after reversing string i.......j
//tot is score without reversal and ntot is with
int tot = 0;
for(int i=0;i<n;i++) {
tot += (s[i] != t[i]);
}
int ntot = c;
reverse(s.begin() + ind.first, s.begin() + ind.second + 1);
for(int i=0;i<n;i++) {
ntot += (s[i] != t[i]);
}
cout << min(ntot, tot);
}
1 Like