Edit Distance Calculation for Documents
Edit Distance Calculation for Documents
68
68
06
00 Parul University
00
00
0
6
26
6
12
12
12
31
03
03
03
0
Name: Rudra Goti Scan to verify results
03
03
03
03
23
23
23
23
Email: 2303031260068@[Link]
Roll no: 2303031260068
Phone: 8200645188
Branch: Parul University
Department: CYBER1_Batch 1
Batch: 2027
Degree: [Link] - Cyber
8
68
8
06
06
06
00
0
60
60
26
26
PIET_DAA_Course
12
12
1
31
03
03
03
30
03
03
03
0
PIET_DAA_Session 9_COD
23
23
23
23
Attempt : 1
Total Mark : 70
Marks Obtained : 70
Section 1 : Coding
1. Problem Statement
8
68
68
06
06
00
00
60
60
6
6
Isabella is part of a team working on a collaborative report. Three team
12
12
12
12
03
03
03
03
03
03
same initial draft of a document. These edits resulted in three different
23
23
23
23
Input Format
The first line of input consists of a string, representing doc1.
8
8
06
06
06
06
60
60
60
60
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
The third line of input consists of a string, representing doc3.
00
00
00
0
6
26
6
12
12
12
Output Format
31
03
03
03
0
03
03
03
03
The first line of output prints "The edit distance between doc1 and doc2: ".
23
23
23
23
The second line of output prints "The edit distance between doc2 and doc3: ".
The third line of output prints "The edit distance between doc1 and doc3: ".
The last line of output print "Minimum total edits needed: ".
8
68
8
06
06
06
00
0
60
60
26
26
12
12
1
31
03
03
03
30
03
03
0
23
23
23
23
Input: program
programming
programing
Output: Edit distance between doc1 and doc2: 4
Edit distance between doc2 and doc3: 1
Edit distance between doc1 and doc3: 3
Minimum total edits needed: 4
Answer
8
68
68
06
06
00
00
#include <stdio.h>
60
60
6
6
#include <string.h>
12
12
12
12
03
03
03
03
#include <stdlib.h>
03
03
03
03
23
23
23
23
8
06
06
06
06
int dp[MAX_LEN][MAX_LEN];
60
60
60
60
12
12
12
03
03
03
03
dp[i][0] = i;
03
03
03
03
23
23
23
23
68
68
68
06
for(int j = 0; j <= len2; j++)
00
00
00
0
dp[0][j] = j;
6
26
6
12
12
12
31
for(int i = 1; i <= len1; i++) {
03
03
03
0
for(int j = 1; j <= len2; j++) {
03
03
03
03
23
23
23
23
if(str1[i-1] == str2[j-1])
dp[i][j] = dp[i-1][j-1];
else
dp[i][j] = 1 + min(dp[i-1][j],
dp[i][j-1],
dp[i-1][j-1]);
}
}
8
68
8
06
06
06
return dp[len1][len2];
00
0
60
60
}
26
26
12
12
1
31
03
03
30
03
03
0
23
23
23
23
int dist23 = editDistance(doc2, doc3);
int dist13 = editDistance(doc1, doc3);
68
68
06
06
00
00
60
60
int main() {
6
6
12
12
12
12
char doc1[MAX_LEN], doc2[MAX_LEN], doc3[MAX_LEN];
03
03
03
03
03
03
03
03
23
23
23
23
8
06
06
06
06
60
60
60
doc3[strcspn(doc3, "\n")] = 0;
12
12
12
12
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
00
00
00
0
return 0;
6
26
6
12
12
12
31
}
03
03
03
0
03
03
03
03
23
23
23
23
Status : Correct Marks : 10/10
2. Problem Statement
68
8
06
06
06
00
minimum edit distance between the spoken text and the written text, while
0
60
60
26
26
12
12
also identifying the specific differences between the two texts. Edit
1
31
03
03
03
operations like insertion, deletion and substitution each cost 1.
30
03
03
03
0
23
23
23
23
Example:
LISTEN
S I L E N T (in this, I remains same and EN remains same other all places
either one of 3 operations has performed, so the minimal cost is 4)
Input Format
8
68
68
06
06
00
00
60
60
The first line of input contains the string, representing the spoken text.
6
6
12
12
12
12
03
03
03
The next line of input contains the string, representing the written text. 03
03
03
03
03
23
23
23
23
Output Format
The output prints the single integer representing the minimal cost.
8
06
06
06
06
60
60
60
60
Input: listen
12
12
12
12
silent
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
Output: 4
00
00
00
0
6
26
6
12
12
12
Answer
31
03
03
03
0
#include <stdio.h>
03
03
03
03
23
23
23
23
#include <string.h>
typedef struct {
char spoken[MAX_LEN];
char written[MAX_LEN];
int spokenLen;
int writtenLen;
8
68
8
06
06
06
} TextComparator;
00
0
60
60
26
26
12
12
1
31
03
03
30
strcpy(tc->spoken, spk);
03
03
03
0
23
23
23
23
strcpy(tc->written, wrt);
tc->spokenLen = strlen(spk);
tc->writtenLen = strlen(wrt);
}
68
68
06
06
}
00
00
60
60
6
6
12
12
12
12
int calculateEditDistance(TextComparator* tc) {
03
03
03
int dp[MAX_LEN][MAX_LEN]; 03
03
03
03
03
23
23
23
23
int i, j;
8
06
06
06
06
60
60
60
60
12
12
12
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
if(tc->spoken[i-1] == tc->written[j-1]) {
00
00
00
0
dp[i][j] = dp[i-1][j-1];
6
26
6
12
12
12
31
} else {
03
03
03
0
int substitute = dp[i-1][j-1] + 1;
03
03
03
03
23
23
23
23
int insert = dp[i][j-1] + 1;
int del = dp[i-1][j] + 1;
dp[i][j] = getMin(substitute, insert, del);
}
}
}
return dp[tc->spokenLen][tc->writtenLen];
}
8
68
8
06
06
06
00
0
60
60
int main() {
26
26
12
12
1
31
TextComparator tc;
03
03
03
30
03
03
0
23
23
23
23
scanf("%s %s", spoken, written);
return 0;
}
8
68
68
06
06
00
60
60
6
6
12
12
12
12
03
03
03
3. Problem Statement 03
03
03
03
03
23
23
23
23
You are given two strings of equal length. You are allowed to move any
character in the first string to the front (like a stack operation) as many
times as needed.
Example:
06
06
06
06
60
60
60
60
Input
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
ADCB //String A
00
00
00
0
6
26
6
12
12
12
ABCD //String B
31
03
03
03
0
03
03
03
03
Output:
23
23
23
23
3
Explanation:
ADCB - CADB //move C to the front
CADB - BCAD // move B to the front
BCAD - ABCD //move A to the front
8
68
8
06
06
06
00
0
60
60
26
26
12
12
Input Format
1
31
03
03
03
30
03
03
03
The first line contains the first string.
0
23
23
23
23
The second line contains the second string.
Output Format
If transformation is possible, print a single integer: the minimum number of
moves required.
68
68
06
06
00
00
60
60
6
12
12
12
12
Input: WYXZ
03
03
03
03
03
03
03
03
WXYZ
23
23
23
23
Output: 2
Answer
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
8
06
06
06
06
60
60
60
60
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
for (int i = 0; first[i]; i++) {
00
00
00
0
freq1[(unsigned char)first[i]]++;
6
26
6
12
12
12
31
freq2[(unsigned char)second[i]]++;
03
03
03
0
}
03
03
03
03
23
23
23
23
for (int i = 0; i < 256; i++) {
if (freq1[i] != freq2[i]) return false;
}
return true;
}
68
8
06
06
06
int count = 0;
00
0
60
60
int i = strlen(first) - 1;
26
26
12
12
1
31
int j = i;
03
03
03
30
03
03
03
0
23
23
23
23
while (i >= 0) {
if (first[i] != second[j]) {
while (i >= 0 && first[i] != second[j]) {
i--;
count++;
}
}
i--;
j--;
8
68
68
06
06
}
00
00
60
60
6
6
12
12
12
12
return count;
03
03
03
} 03
03
03
03
03
23
23
23
23
int main() {
char first[101], second[101];
scanf("%s", first);
scanf("%s", second);
if (isTransformable(first, second)) {
printf("%d\n", getMinimumOperations(first, second));
} else {
8
8
06
06
06
06
60
60
60
}
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
return 0;
00
00
00
0
}
6
26
6
12
12
12
31
03
03
03
0
Status : Correct Marks : 10/10
03
03
03
03
23
23
23
23
4. Problem Statement
You are given a string. Your task is to form the longest possible palindrome
by rearranging or deleting characters from the given string.
68
8
06
06
06
You can use any number of characters from the input.
00
0
60
60
26
26
12
12
Only one character with odd frequency can be placed in the middle of the
1
31
03
03
03
30
palindrome.
03
03
03
0
23
23
23
23
Example:
Input:
ABBDAB
Output:
BABAB
8
68
68
06
06
00
00
60
60
Input Format
6
6
12
12
12
12
03
03
03
03
03
03
English letters.
23
23
23
23
Output Format
The output prints the longest palindrome that can be formed from the characters
of the given string.
Sample Test Case
Input: BCDBD
Output: BDCDB
8
8
06
06
06
06
Answer
60
60
60
60
12
12
12
12
#include <stdio.h>
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
#include <string.h>
00
00
00
0
6
26
6
12
12
12
31
int main() {
03
03
03
0
char s[101];
03
03
03
03
23
23
23
23
scanf("%100s", s);
68
8
06
06
06
00
0
60
60
char left[101] = {0};
26
26
12
12
1
31
int left_index = 0;
03
03
03
30
char mid = 0;
03
03
03
0
23
23
23
23
for (int c = 0; c < 256; c++) {
if (freq[c] > 0) {
if (freq[c] % 2 == 1) {
mid = (char)c;
}
68
68
06
06
left[left_index++] = (char)c;
00
00
60
60
}
6
6
12
12
12
12
}
03
03
03
} 03
03
03
03
03
23
23
23
23
left[left_index] = '\0';
printf("%s", left);
if (mid != 0) {
printf("%c", mid);
}
8
8
06
06
06
06
60
60
60
printf("%c", left[i]);
12
12
12
12
}
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
printf("\n");
00
00
00
0
6
26
6
12
12
12
31
return 0;
03
03
03
0
}
03
03
03
03
23
23
23
23
Status : Correct Marks : 10/10
5. Problem Statement
68
8
06
06
06
00
corrections. The core of this feature is calculating how close the typed
0
60
60
26
26
word is to a valid dictionary word. You are tasked with writing a program
12
12
1
31
03
03
03
that computes the minimum number of operations required to convert one
30
03
03
03
0
23
23
23
Insert a characterRemove a characterReplace a character
Input Format
The first line of input contains the string word1 representing the text by user.
The next line of input contains the string word2 representing the text from
dictionary.
8
68
68
06
06
Output Format
00
00
60
60
6
6
The output prints the single integer representing the the minimum number of
12
12
12
12
03
03
03
03
operations required to convert word1 to word2.
03
03
03
03
23
23
23
23
Output: 3
06
06
06
06
60
60
60
60
Answer
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
#include <iostream>
00
00
00
0
#include <vector>
6
26
6
12
12
12
31
#include <string>
03
03
03
0
#include <algorithm>
03
03
03
03
23
23
23
23
using namespace std;
68
8
06
06
06
for (int i = 0; i <= n; i++) dp[i][0] = i;
00
0
60
60
for (int j = 0; j <= m; j++) dp[0][j] = j;
26
26
12
12
1
31
03
03
03
30
03
03
03
0
23
23
23
23
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (word1[i-1] == word2[j-1]) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = 1 + min({
min(dp[i-1][j], dp[i][j-1]),
dp[i-1][j-1]
});
8
68
68
06
06
}
00
00
60
60
}
6
6
12
12
12
12
}
03
03
03
03
03
03
03
03
23
23
23
23
return dp[n][m];
}
int main() {
string word1, word2;
cin >> word1 >> word2;
8
06
06
06
06
}
60
60
60
60
12
12
12
12
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
6. Problem Statement
00
00
00
0
6
26
6
12
12
12
31
Alex is a student at St. Joseph's High School and is working on a project to
03
03
03
0
03
03
03
03
convert one version of a document into another. In order to make the two
23
23
23
23
versions identical, Alex can perform three types of operations: insertion,
deletion, and substitution. Each operation comes with a cost, and Alex
wants to minimize the total cost of transforming the document. The costs
of each operation are as follows:
Insertion (adding a character): 3 units of costDeletion (removing a
character): 2 units of costSubstitution (replacing a character): 5 units of
cost
8
68
8
Alex needs your help to determine the minimum cost required to transform
06
06
06
00
0
60
60
the first document (doc1) into the second document (doc2) using a
26
26
12
12
1
31
combination of these operations. The goal is to figure out how Alex can
03
03
03
30
03
03
03
make the transformation at the lowest possible cost.
0
23
23
23
23
Input Format
The first input is a string doc1, representing the original document.
68
68
transform doc1 into doc2.
06
06
00
00
60
60
6
6
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
8
06
06
06
06
#include <stdio.h>
60
60
60
60
#include <string.h>
12
12
12
12
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
typedef struct {
00
00
00
0
char doc1[MAX_LEN];
6
26
6
12
12
12
31
char doc2[MAX_LEN];
03
03
03
0
int len1;
03
03
03
03
23
23
23
23
int len2;
} DocTransform;
void initTransformer(DocTransform* dt, char* str1, char* str2) {
strcpy(dt->doc1, str1);
strcpy(dt->doc2, str2);
dt->len1 = strlen(str1);
dt->len2 = strlen(str2);
}
int getMin(int a, int b, int c) {
8
68
8
06
06
06
if (a <= b && a <= c) return a;
00
0
60
60
if (b <= a && b <= c) return b;
26
26
12
12
1
31
return c;
03
03
03
30
}
03
03
03
0
23
23
23
23
int calculateMinCost(DocTransform* dt) {
int dp[MAX_LEN][MAX_LEN];
int i, j;
const int INS_COST = 3;
const int DEL_COST = 2;
const int SUB_COST = 5;
68
68
06
06
dp[0][j] = j * INS_COST;
00
00
60
60
}
6
6
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
if(dt->doc1[i-1] == dt->doc2[j-1]) {
8
8
06
06
06
06
dp[i][j] = dp[i-1][j-1];
60
60
60
60
}
12
12
12
12
else {
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
00
00
00
0
int substitute = dp[i-1][j-1] + SUB_COST;
6
26
6
12
12
12
31
int insert = dp[i][j-1] + INS_COST;
03
03
03
0
int del = dp[i-1][j] + DEL_COST;
03
03
03
03
23
23
23
23
dp[i][j] = getMin(substitute, insert, del);
}
}
}
return dp[dt->len1][dt->len2];
}
8
68
8
06
06
06
int main() {
00
0
60
60
DocTransform dt;
26
26
12
12
1
31
03
03
30
int scan_result;
03
03
03
0
23
23
23
23
scan_result = scanf("%s", str1);
scan_result = scanf("%s", str2);
68
68
06
06
printf("%d", minCost);
00
00
60
60
6
6
12
12
12
12
return 0;
03
03
03
} 03
03
03
03
03
23
23
23
23
7. Problem Statement
8
06
06
06
06
60
60
60
characters, each with a specific cost. The cost of each operation is given
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
as input. Write a program to calculate the minimum cost of transforming
00
00
00
0
one manuscript into another.
6
26
6
12
12
12
31
03
03
03
0
03
03
03
03
Input Format
23
23
23
23
The first line of input contains the string m1, representing manuscript 1.
The second line of input contains the string m2, representing manuscript 2.
The third line of input contains three integers representing the costs of the
operations: insertion, deletion, and substitution.
Output Format
8
68
8
The output prints the integer representing the minimum cost required to
06
06
06
00
0
60
60
transform manuscript1 into manuscript2.
26
26
12
12
1
31
03
03
03
30
03
03
03
0
23
23
23
23
Refer to the sample output for formatting specifications.
Sample Test Case
Input: book
back
4
3
8
68
68
5
06
06
00
00
60
60
6
6
Output: 10
12
12
12
12
03
03
03
Answer 03
03
03
03
03
23
23
23
23
#include <stdio.h>
#include <string.h>
#define MAX_LEN 101
typedef struct {
char text1[MAX_LEN];
char text2[MAX_LEN];
int len1;
int len2;
int insCost;
8
8
06
06
06
06
int delCost;
60
60
60
60
int subCost;
12
12
12
12
} Manuscript;
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
void initManuscript(Manuscript* m, char* str1, char* str2, int ins, int del, int sub)
00
00
00
0
{
6
26
6
12
12
12
31
strcpy(m->text1, str1);
03
03
03
0
strcpy(m->text2, str2);
03
03
03
03
23
23
23
23
m->len1 = strlen(str1);
m->len2 = strlen(str2);
m->insCost = ins;
m->delCost = del;
m->subCost = sub;
}
68
8
06
06
06
if (b <= a && b <= c) return b;
00
0
60
60
return c;
26
26
12
12
1
31
}
03
03
03
30
03
03
03
0
23
23
23
23
int calculateMinCost(Manuscript* m) {
int dp[MAX_LEN][MAX_LEN];
int i, j;
for(j = 0; j <= m->len2; j++) {
dp[0][j] = j * m->insCost;
}
for(i = 0; i <= m->len1; i++) {
dp[i][0] = i * m->delCost;
}
8
68
68
06
06
00
60
60
6
12
12
12
12
if(m->text1[i-1] == m->text2[j-1]) {
03
03
03
dp[i][j] = dp[i-1][j-1]; 03
03
03
03
03
23
23
23
23
} else {
int substitute = dp[i-1][j-1] + m->subCost;
int insert = dp[i][j-1] + m->insCost;
int del = dp[i-1][j] + m->delCost;
8
06
06
06
06
60
60
60
60
return dp[m->len1][m->len2];
12
12
12
12
}
03
03
03
03
03
03
03
03
23
23
23
23
68
68
68
06
00
00
00
0
int main() {
6
26
6
12
12
12
31
Manuscript m;
03
03
03
0
char str1[MAX_LEN], str2[MAX_LEN];
03
03
03
03
23
23
23
23
int insCost, delCost, subCost;
scanf("%s", str1);
scanf("%s", str2);
scanf("%d", &insCost);
scanf("%d", &delCost);
scanf("%d", &subCost);
initManuscript(&m, str1, str2, insCost, delCost, subCost);
int minCost = calculateMinCost(&m);
printf("%d", minCost);
8
68
8
06
06
06
00
0
60
60
return 0;
26
26
12
12
1
31
}
03
03
03
30
03
03
03
0
23
23
23
23
Status : Correct Marks : 10/10
8
68
68
06
06
00
00
60
60
6
6
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23
8
8
06
06
06
06
60
60
60
60
12
12
12
12
03
03
03
03
03
03
03
03
23
23
23
23