|
#include <iostream> |
|
#include <vector> |
|
using namespace std; |
|
|
|
using vi = vector<int>; |
|
|
|
bool almost_perfect(const vi &big, const vi &small) { |
|
int diff = 0; |
|
for (int c = 0; c < 26; c++) { |
|
if (big[c] < small[c]) { |
|
return false; |
|
} |
|
diff += big[c] - small[c]; |
|
} |
|
return diff == 1; |
|
} |
|
|
|
int solve() { |
|
string S; |
|
cin >> S; |
|
vector<vi> pre(S.size() + 1, vi(26)); |
|
for (int i = 1; i <= (int)S.size(); i++) { |
|
pre[i] = pre[i - 1]; |
|
pre[i][S[i - 1] - 'a']++; |
|
} |
|
int Q; |
|
cin >> Q; |
|
int ans = 0; |
|
for (int i = 0, l, r; i < Q; i++) { |
|
cin >> l >> r; |
|
--l, --r; |
|
if ((r - l + 1) % 2 == 0) { |
|
continue; |
|
} |
|
auto get_freqs = [&](int l, int r) { |
|
vi res; |
|
for (int c = 0; c < 26; c++) { |
|
res.push_back(pre[r + 1][c] - pre[l][c]); |
|
} |
|
return res; |
|
}; |
|
int m = (l + r) / 2; |
|
if (almost_perfect(get_freqs(l, m), get_freqs(m + 1, r)) || |
|
almost_perfect(get_freqs(m, r), get_freqs(l, m - 1))) { |
|
ans++; |
|
} |
|
} |
|
return ans; |
|
} |
|
|
|
int main() { |
|
ios_base::sync_with_stdio(false); |
|
cin.tie(nullptr); |
|
int T; |
|
cin >> T; |
|
for (int t = 1; t <= T; t++) { |
|
cout << "Case #" << t << ": " << solve() << endl; |
|
} |
|
return 0; |
|
} |
|
|