C++ アルゴリズムとデータ構造のライブラリ
#include "library/string/run_length.hpp"AAAABBBCDDDD
↓
{ [A, 4], [B, 3], [C, 1], [D, 4] }
$O(\vert S \vert)$
vector<pair<char, int>> R = run_length(S);
#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;
}