serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: ランレングス圧縮
(library/string/run_length.hpp)

ランレングス圧縮

できること

計算量

$O(\vert S \vert)$

使い方

vector<pair<char, int>> R = run_length(S);

Verified with

Code

#pragma once
vector<pair<char, int>> run_length(const string &S) {
    vector<pair<char, int>> res;
    for (auto &&x : S) {
        if (res.empty() or res.back().first != x) res.emplace_back(x, 0);
        ++res.back().second;
    }
    return res;
}
#line 2 "library/string/run_length.hpp"
vector<pair<char, int>> run_length(const string &S) {
    vector<pair<char, int>> res;
    for (auto &&x : S) {
        if (res.empty() or res.back().first != x) res.emplace_back(x, 0);
        ++res.back().second;
    }
    return res;
}
Back to top page