Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language : C++
- */
- #include<bits/stdc++.h>
- using namespace std;
- char a[110],b[110],c[110];
- int dp[110][110][110];
- int main(){
- int q;
- cin >> q;
- while(q--){
- cin >> a+1 >> b+1 >> c+1;
- int la = strlen(a+1),lb = strlen(b+1),lc = strlen(c+1);
- // base cases
- for(int i=0;i<=la;i++)
- for(int j=0;j<=lb;j++)
- dp[i][j][0] = i+j;
- for(int k=0;k<=lc;k++)
- dp[0][0][k] = k;
- // process
- for(int k=1;k<=lc;k++){
- for(int i=0;i<=la;i++){
- for(int j=0;j<=lb;j++){
- if(i == 0 && j == 0) continue;
- // Insert
- dp[i][j][k] = dp[i][j][k-1]+1;
- // Remove and Replace (string a)
- if(i-1 >= 0)
- dp[i][j][k] = min({dp[i][j][k],dp[i-1][j][k]+1,dp[i-1][j][k-1]+(a[i]!=c[k])});
- // Remove and Replace (string b)
- if(j-1 >= 0)
- dp[i][j][k] = min({dp[i][j][k],dp[i][j-1][k]+1,dp[i][j-1][k-1]+(b[j]!=c[k])});
- }
- }
- }
- cout << dp[la][lb][lc] << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement