Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GROUP: 3
- // ID: 20150202
- // Assign: 11
- // Assign: EditDist
- // UVA: 526
- // Name: Mohamed Ahmed Abdelmonim
- // UVA Username: mmn3mm
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- using namespace std;
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- char a[82];
- char b[82];
- int D[81][81]; // D[][] is the same for all versions (no memory reduction)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int T1[81][81];
- int EditDist1(int n, int m)
- {
- if (T1[n][m] != -1)return T1[n][m];
- if (n == 0 || m == 0)
- {
- T1[n][m] = n + m;
- return n + m;
- }
- int dummy = 1;
- if (a[n-1] == b[m-1])dummy = 0;
- int replace = dummy + EditDist1(n - 1, m - 1);
- int remove = 1 + EditDist1(n - 1, m);
- int insert = 1 + EditDist1(n, m - 1);
- D[n][m] = 2; //That means we assume it's insert unless changed.
- cout << "n:" << n << " m:" << m << endl;
- int ret = insert;
- cout << "Replace " << replace << " ";
- cout << "Delete " << remove << " ";
- cout << "INSERT " << insert << " ";
- if (replace < insert)
- {
- ret = replace;
- D[n][m] = 1; //Changed to replace.
- }
- if (ret > remove){
- ret = remove;
- D[n][m] = 3;//Changed to remove
- }
- cout << endl;
- cout << "CHOSEN " << ret << " ";
- cout << endl;
- T1[n][m] = ret;
- return ret;
- }
- int ComputeEditDist1(int N, int M){ // Recursive - memoization - initialize T then call EditDist1(N,M);
- for (int i = 0; i <= N; i++)
- {
- for (int j = 0; j <=M; j++)
- T1[i][j] = -1;
- }
- EditDist1(N, M);
- return T1[N][M];
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int T2[81][81];
- int ComputeEditDist2(int N, int M); // Bottom-up, do not save space
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int T3[2][81];
- int ComputeEditDist3(int N, int M); // Bottom-up, use two rows only
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int T4[81];
- int ComputeEditDist4(int N, int M); // Bottom-up, save maximum space
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- struct Oper
- {
- int n, m; //1: replace , 2: Insert, 3:Remove
- int oper;
- };
- Oper opers[81];
- int EditDistSol(int N, int M){ // Print the solution using D[][]
- int n = N, m = M;
- int i = 0;
- while (n>0 && m>0)
- {
- if (D[n][m] == 3) //remove
- {
- Oper oper;
- oper.oper = 3;
- oper.n = n;
- n -= 1;
- opers[i++] = oper;
- continue;
- }
- else if (D[n][m] == 2) //insert
- {
- Oper oper;
- oper.oper = 2;
- oper.n = n;
- oper.m = b[m - 1];
- m-= 1;
- opers[i++] = oper;
- continue;
- }
- else if (D[n][m] == 1 && a[n - 1] == b[m - 1]) //replace
- {
- Oper oper;
- oper.oper = 3;
- oper.n = n;
- oper.m = b[m-1];
- n -= 1;
- m -= 1;
- opers[i++] = oper;
- continue;
- }
- n--;
- m--;
- }
- while (true)
- {
- if (n == 0 && m == 0)break;
- if (n == 0 && m != 0)
- {
- Oper oper;
- oper.oper = 2;
- oper.n = n;
- m -= 1;
- opers[i++] = oper;
- }
- if (n != 0 && m == 0)
- {
- Oper oper;
- oper.oper = 3;
- oper.n = n;
- n -= 1;
- opers[i++] = oper;
- }
- }
- int c = 0;
- for (int j = i-1; j >= 0; j--)
- {
- c++;
- cout << c <<" ";
- Oper curr = opers[j];
- switch (curr.oper){
- case 1:
- cout << "Replace " << curr.n << "," << char(curr.m);
- break;
- case 2:
- cout << "Insert " << curr.n << "," << char(curr.m);
- break;
- case 3:
- cout << "Delete " << curr.n;
- break;
- }
- cout << endl;
- }
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int ComputeEditDist(int N, int M) // Here we can choose the method
- {
- return ComputeEditDist1(N,M);
- //return ComputeEditDist2(N,M);
- //return ComputeEditDist3(N,M);
- //return ComputeEditDist4(N, M);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // This function does not need any modification:
- void Compute() // Get input and call ComputeEditDist() whenever needed
- {
- int cas = 0;
- while (true)
- {
- a[0] = 0; b[0] = 0;
- if (!fgets(a, 82, stdin)) break; fgets(b, 82, stdin);
- a[strlen(a) - 1] = 0;
- b[strlen(b) - 1] = 0;
- if (cas) cout << endl; // print an empty line between test cases
- int N = strlen(a), M = strlen(b);
- ComputeEditDist(N, M);
- EditDistSol(N, M);
- cas++;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int main()
- {
- //freopen("input.txt", "r", stdin);
- Compute();
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement