C++ Library for Competitive Programming
#include "emthrm/string/subsequence_dp.hpp"
$O(\sigma \lvert S \rvert)$
名前 | 戻り値 |
---|---|
std::vector<std::vector<int>> subsequence_dp(const std::string& s, const char basis = 'a', const int sigma = 26); |
$S$ の $i$ 文字目以降 (inclusive) で最初に文字 $c$ が現れるインデックス |
https://atcoder.jp/contests/arc081/submissions/26043202
#ifndef EMTHRM_STRING_SUBSEQUENCE_DP_HPP_
#define EMTHRM_STRING_SUBSEQUENCE_DP_HPP_
#include <algorithm>
#include <string>
#include <vector>
namespace emthrm {
std::vector<std::vector<int>> subsequence_dp(
const std::string& s, const char basis = 'a', const int sigma = 26) {
const int n = s.length();
std::vector<std::vector<int>> nx(n, std::vector<int>(sigma, n));
nx[n - 1][s[n - 1] - basis] = n - 1;
for (int i = n - 2; i >= 0; --i) {
std::copy(nx[i + 1].begin(), nx[i + 1].end(), nx[i].begin());
nx[i][s[i] - basis] = i;
}
return nx;
}
} // namespace emthrm
#endif // EMTHRM_STRING_SUBSEQUENCE_DP_HPP_
#line 1 "include/emthrm/string/subsequence_dp.hpp"
#include <algorithm>
#include <string>
#include <vector>
namespace emthrm {
std::vector<std::vector<int>> subsequence_dp(
const std::string& s, const char basis = 'a', const int sigma = 26) {
const int n = s.length();
std::vector<std::vector<int>> nx(n, std::vector<int>(sigma, n));
nx[n - 1][s[n - 1] - basis] = n - 1;
for (int i = n - 2; i >= 0; --i) {
std::copy(nx[i + 1].begin(), nx[i + 1].end(), nx[i].begin());
nx[i][s[i] - basis] = i;
}
return nx;
}
} // namespace emthrm