解决思路
使用后缀和计算到 i 处共有多少对 \(s_i = s_{i+1}\),计算时相减以下就可以
#include <bits/stdc++.h>
int main() {
std::string s;
int m;
std::cin >> s >> m;
std::vector<std::vector<int>> vec(m, std::vector<int>(2, 0));
for (int i = 0; i < m; ++i) std::cin >> vec[i][0] >> vec[i][1];
std::vector<int> rec(s.size() + 1, 0);
for (int i = s.size() - 1; i > 0; --i) {
rec[i] += rec[i + 1];
if (s[i - 1] == s[i]) ++rec[i];
}
for (auto &v : vec) {
std::cout << rec[v[0]] - rec[v[1]] << std::endl;
}
}
标签:std,int,Ilya,vector,vec,Queries,rec,Problem,size
From: https://www.cnblogs.com/HelloEricy/p/17467886.html