Link Search Menu Expand Document
あるまかんライブラリ

:heavy_check_mark: test/helloworld/runlength.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <bits/stdc++.h>

#include "../../Util/runlength.hpp"

using namespace std;

void test1() {
    array<int, 6> a{1, 1, 5, 5, 5, -8};

    const vector<pair<decltype(a)::iterator, size_t>> expected = {
        {begin(a) + 0, size_t(2)},
        {begin(a) + 2, size_t(3)},
        {begin(a) + 5, size_t(1)},
    };

    assert(runlength(begin(a), end(a)) == expected);
    clog << __func__ << " : OK" << endl;
}

void test2() {
    array<int, 1> a{1727};

    const vector<pair<decltype(a)::iterator, size_t>> expected = {
        {begin(a) + 0, size_t(1)},
    };

    assert(runlength(begin(a), end(a)) == expected);
    clog << __func__ << " : OK" << endl;
}

void test3() {
    string s = "x01ssss";

    const vector<pair<decltype(s)::iterator, size_t>> expected = {
        {begin(s) + 0, size_t(1)},
        {begin(s) + 1, size_t(1)},
        {begin(s) + 2, size_t(1)},
        {begin(s) + 3, size_t(4)},
    };

    assert(runlength(begin(s), end(s)) == expected);
    clog << __func__ << " : OK" << endl;
}

int main() {
    std::cout << "Hello World" << std::endl;

    test1();
    test2();
    test3();

    return 0;
}
#line 1 "test/helloworld/runlength.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <bits/stdc++.h>

#line 5 "Util/runlength.hpp"

/**
 * @brief runlength() (ランレングス圧縮)
 * (かたまりの始まり位置イテレータ, かたまりの要素数) のpairのvectorを返す。
 */
template <class Iterator>
auto runlength(Iterator const begin, Iterator const end) {
    std::vector<std::pair<Iterator, size_t>> ret;
    ret.reserve(std::distance(begin, end));

    for (auto itr = begin; itr != end;) {
        size_t cnt = 1;
        const auto head = itr++;
        while (itr != end && *itr == *head) ++itr, ++cnt;
        ret.emplace_back(head, cnt);
    }

    ret.shrink_to_fit();
    return ret;
}
#line 5 "test/helloworld/runlength.test.cpp"

using namespace std;

void test1() {
    array<int, 6> a{1, 1, 5, 5, 5, -8};

    const vector<pair<decltype(a)::iterator, size_t>> expected = {
        {begin(a) + 0, size_t(2)},
        {begin(a) + 2, size_t(3)},
        {begin(a) + 5, size_t(1)},
    };

    assert(runlength(begin(a), end(a)) == expected);
    clog << __func__ << " : OK" << endl;
}

void test2() {
    array<int, 1> a{1727};

    const vector<pair<decltype(a)::iterator, size_t>> expected = {
        {begin(a) + 0, size_t(1)},
    };

    assert(runlength(begin(a), end(a)) == expected);
    clog << __func__ << " : OK" << endl;
}

void test3() {
    string s = "x01ssss";

    const vector<pair<decltype(s)::iterator, size_t>> expected = {
        {begin(s) + 0, size_t(1)},
        {begin(s) + 1, size_t(1)},
        {begin(s) + 2, size_t(1)},
        {begin(s) + 3, size_t(4)},
    };

    assert(runlength(begin(s), end(s)) == expected);
    clog << __func__ << " : OK" << endl;
}

int main() {
    std::cout << "Hello World" << std::endl;

    test1();
    test2();
    test3();

    return 0;
}
Back to top page