ZeroJudge K926: Correction

小明最近在練習英文打字,每次練習後,系統會幫小明計算差異量。差異量計算方式為小明所打文字與正確文字相對位置不同的英文字母數目,例如 mouse 和 house 之間的差異量為 1。 他經常忘記英文字母在鍵盤上的位置,所以常常打錯字。

The system offers a "one-to-one" correction feature, where each correction replaces all occurrences of one specified letter with another specified letter in the string. Once corrected, the text cannot be restored, meaning that the result of the first correction serves as the basis for subsequent corrections.

For example, if the correct word is "ccdd" and the text typed by Ming is "aabb", and the system executes the correction "replace 'a' with 'c'", then "aabb" will be corrected to "ccbb". If the system then immediately executes the correction "replace 'a' with 'g'", there will be no change, because the current text has already become "ccbb", which does not contain any 'a'.

Please write a program to calculate the difference after correction of the text typed by Ming.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first and second lines each contain a string S1 and S2 (only lowercase English letters, with a length not exceeding 100, and the lengths of S1 and S2 are guaranteed to be equal), representing the text typed by Ming and the correct text, respectively. The third line contains an integer N (1 ≤ N ≤100) indicating the number of corrections. Following that, there are N lines, each containing two characters Xi and Yi (1 ≤ i ≤ N), indicating the replacement of the character Xi with Yi in the current text. The current text refers to the text obtained from S1 after several corrections.Output the distance between the two strings after correction (the difference after correction).
aabb
ccdd
1
a c
2
aabb
ccdd
2
a b
b c
2
time
game
2
i a
t g
0
elephant
kangaroo
3
e o
p k
a n
8

Thought Process

Use a for loop to determine the current character. If it is Xi, replace the current character with Yi.

After replacing the characters, compare the characters at the same position in the two different strings. If they are not the same, increment the answer by 1.

Sample Code-ZeroJudge K926: Correction

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    string input, correct;
    int N;
    cin >> input >> correct >> N;
    for (int i = 0; i<N; i++) {
        char a, b;
        cin >> a >> b;
        for (int j = 0; j<input.length(); j++) {
            if (input[j] == a) input[j] = b;
        }
    }
    int ans = 0;
    for (int i = 0; i<input.length(); i++) {
        if (input[i] != correct[i]) ans++;
    }
    cout << ans << "\n";
}

//ZeroJudge K926
//Dr. SeanXD

Comments