Official

C - XX to XXX Editorial by en_translator


Consider decomposing the given string into (maximal) segments consisting of the same characters and expressing each segment as “how many copies of what character.” For example, a string aaabbbbaccc is decomposed to a segment consisting of \(3\) copies of a, \(4\) of b, \(1\) of a, and \(3\) of c. Let us denote this by aaabbbbaccc = \((\) a \(, 3)(\) b \(, 4)(\) a \(, 1)(\) c \(, 3)\). In general, a string \(X\) can be represented by non-negative integer \(n\), characters \(c_1, c_2, \ldots, c_n\), and positive integers \(l_1, l_2, \ldots, l_n\) as

\[X = (c_1, l_1)(c_2, l_2)\ldots (c_n, l_n)\]

(where \(c_i \neq c_{i+1}\) for all \(i = 1, 2, \ldots, n-1\)). Such an operation of expressing a string by a sequence of “segments of how many copies of what characters” is called run-length encoding.

Suppose that the given strings \(S\) and \(T\) are represented as

\[S = (c_1, l_1)(c_2, l_2)\ldots (c_n, l_n), \tag{1}\]

\[T = (c'_1, l'_1)(c'_2, l'_2)\ldots (c'_{n'}, l'_{n'}). \tag{2}\]

Every time an operation in the Problem Statement is performed, in the segment of \(S\) consisting of two or more copies of the same character, a new copy of the character is inserted. Thus, the operation in the Problem Statement can be regarded as “an operation of choosing an integer \(i\) between \(1\) and \(n\) such that \(l_i \geq 2\) and increasing the value of \(l_i\) by \(1\).” One can make \(S\) equal \(T\) if and only if the following three conditions are satisfied:

  1. \(n = n'\)
  2. \(c_i = c'_i\) for all \(i = 1, 2, \ldots, n\)
  3. One of the following holds for all \(i = 1, 2, \ldots, n\):
    • \(l_i = l'_i\)
    • \(l_i \lt l'_i\) and \(l_i \geq 2\)

Therefore, in order to solve this problem,

  • first compute the run-length encoding of \(S\) and \(T\) to obtain the expression (1) and (2),
  • and then check if these satisfy all of the conditions from 1. to 3. described above.

Both of them can be done in a total of \(\mathrm{O}(|S|+|T|)\) time, so the problem can be solved in a total of \(\mathrm{O}(|S|+|T|)\) time.

The following is a sample code for this problem in C++ language:

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

void rle(string s, vector<pair<char, int>> &vec)
{
  int cnt = 1;
  for(int i = 1; i < (int)s.size(); i++){
    if(s[i] != s[i-1]){
      vec.push_back({s[i-1], cnt});
      cnt = 0;
    }
    cnt++;
  }
  vec.push_back({s.back(), cnt});
}

int main(void)
{
  string s, t;  
  cin >> s >> t;
  
  vector<pair<char, int>> svec, tvec;
  rle(s, svec), rle(t, tvec);

  if(svec.size() != tvec.size()){
    cout << "No" << endl;
    return 0;
  }

  bool ans = true;
  for(int i = 0; i < svec.size(); i++){
    if(svec[i].first != tvec[i].first) ans = false;
    if(!(svec[i].second == tvec[i].second || svec[i].second < tvec[i].second && svec[i].second >= 2)) ans = false;
  }
  if(ans) cout << "Yes" << endl;
  else cout << "No" << endl;

  return 0;
}

posted:
last update: