proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/run_length.test.cpp

Depends on

Code

// competitive-verifier: STANDALONE

#include <cstdlib>
#include <iostream>
#include <vector>
#include <iterator>
#include "tools/assert_that.hpp"
#include "tools/run_length.hpp"

using ll = long long;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  std::string s;
  std::vector<std::pair<char, ll>> v;

  s = "ABBCCC";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 3);
  assert_that(v[0].first == 'A');
  assert_that(v[0].second == 1);
  assert_that(v[1].first == 'B');
  assert_that(v[1].second == 2);
  assert_that(v[2].first == 'C');
  assert_that(v[2].second == 3);

  s = "AAABBC";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 3);
  assert_that(v[0].first == 'A');
  assert_that(v[0].second == 3);
  assert_that(v[1].first == 'B');
  assert_that(v[1].second == 2);
  assert_that(v[2].first == 'C');
  assert_that(v[2].second == 1);

  s = "";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 0);

  s = "A";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 1);
  assert_that(v[0].first == 'A');
  assert_that(v[0].second == 1);

  return 0;
}
#line 1 "tests/run_length.test.cpp"
// competitive-verifier: STANDALONE

#include <cstdlib>
#include <iostream>
#include <vector>
#include <iterator>
#line 1 "tools/assert_that.hpp"



#line 6 "tools/assert_that.hpp"

#define assert_that_impl(cond, file, line, func) do {\
  if (!cond) {\
    ::std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
    ::std::exit(EXIT_FAILURE);\
  }\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)


#line 1 "tools/run_length.hpp"



#line 5 "tools/run_length.hpp"
#include <cstddef>
#include <utility>
#include <cstdint>

namespace tools {
  template <typename InputIterator, typename OutputIterator>
  void run_length(const InputIterator& begin, const InputIterator& end, OutputIterator result) {
    using T = typename ::std::iterator_traits<InputIterator>::value_type;
    if (begin == end) return;

    ::std::pair<T, ::std::size_t> prev;
    for (auto [it, breaks] = ::std::make_pair(begin, false); !breaks; breaks = it == end, it = ::std::next(it, breaks ? 0 : 1)) {
      bool flg1, flg2;
      if (it == begin) {
        flg1 = false;
        flg2 = true;
      } else if (it == end) {
        flg1 = true;
        flg2 = false;
      } else if (*it != prev.first) {
        flg1 = true;
        flg2 = true;
      } else {
        flg1 = false;
        flg2 = false;
      }
      if (flg1 || flg2) {
        if (flg1) {
          *result = prev;
          ++result;
        }
        if (flg2) {
          prev.first = *it;
          prev.second = 1;
        }
      } else {
        ++prev.second;
      }
    }
  }
}


#line 9 "tests/run_length.test.cpp"

using ll = long long;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  std::string s;
  std::vector<std::pair<char, ll>> v;

  s = "ABBCCC";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 3);
  assert_that(v[0].first == 'A');
  assert_that(v[0].second == 1);
  assert_that(v[1].first == 'B');
  assert_that(v[1].second == 2);
  assert_that(v[2].first == 'C');
  assert_that(v[2].second == 3);

  s = "AAABBC";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 3);
  assert_that(v[0].first == 'A');
  assert_that(v[0].second == 3);
  assert_that(v[1].first == 'B');
  assert_that(v[1].second == 2);
  assert_that(v[2].first == 'C');
  assert_that(v[2].second == 1);

  s = "";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 0);

  s = "A";
  v.clear();
  tools::run_length(s.begin(), s.end(), std::back_inserter(v));
  assert_that(v.size() == 1);
  assert_that(v[0].first == 'A');
  assert_that(v[0].second == 1);

  return 0;
}
Back to top page