Official

B - Spot the Difference Editorial by en_translator


This problem requires processing strings and using a for statement. If you could not solve this problem, review how to use strings.

This problem can be solved by scanning the entire grid and discovering different characters in \(A\) and \(B\). Specifically, it is sufficient to implement the following algorithm.

  • Do the following for each \(i = 1, 2, \dots, N\) in order:
    • Do the following for each \(j = 1, 2, \dots, N\) in order:
      • Check if \(A_{i, j}\) and \(B_{i, j}\) are the same character.
        • If they are the same, do nothing.
        • If they are different, print \((i, j)\) and terminate the entire process.

The complexity is \(\mathrm{O}(N^2)\), which is fast enough.

  • Sample code (C++)
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<string> A(N), B(N);
  for (auto& s : A) cin >> s;
  for (auto& s : B) cin >> s;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[i][j] != B[i][j]) {
        cout << i + 1 << " " << j + 1 << endl;
        return 0;
      }
    }
  }
}

posted:
last update: