cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:warning: 文字列/ランレングス圧縮
(test/string/run_length_encoding.test.cpp)

Depends on

Code

/*
 * @title 文字列/ランレングス圧縮
 *
 * verification-helper: IGNORE
 * verification-helper: PROBLEM https://atcoder.jp/contests/abc143/tasks/abc143_c
 */

#include <iostream>
#include <string>

#include "emthrm/string/run_length_encoding.hpp"

int main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  std::cout << emthrm::run_length_encoding(s).size() << '\n';
  return 0;
}
#line 1 "test/string/run_length_encoding.test.cpp"
/*
 * @title 文字列/ランレングス圧縮
 *
 * verification-helper: IGNORE
 * verification-helper: PROBLEM https://atcoder.jp/contests/abc143/tasks/abc143_c
 */

#include <iostream>
#include <string>

#line 1 "include/emthrm/string/run_length_encoding.hpp"



#include <utility>
#include <vector>

namespace emthrm {

template <typename T>
requires requires { typename T::value_type; }
std::vector<std::pair<typename T::value_type, int>> run_length_encoding(
    const T& s) {
  const int n = s.size();
  std::vector<std::pair<typename T::value_type, int>> res;
  if (n == 0) [[unlikely]] return res;
  typename T::value_type ch = s.front();
  int num = 1;
  for (int i = 1; i < n; ++i) {
    if (s[i] != ch) {
      res.emplace_back(ch, num);
      num = 0;
    }
    ch = s[i];
    ++num;
  }
  res.emplace_back(ch, num);
  return res;
}

}  // namespace emthrm


#line 12 "test/string/run_length_encoding.test.cpp"

int main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  std::cout << emthrm::run_length_encoding(s).size() << '\n';
  return 0;
}
Back to top page