-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_Sort.cpp
49 lines (33 loc) · 1006 Bytes
/
C_Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// RunAt - Green
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n,q; cin >> n >> q;
string a,b;
cin >> a >> b;
vector<vector<int>> freqA(n+1, vector<int>(26));
vector<vector<int>> freqB(n+1, vector<int>(26));
for (int i=0; i<n; i++) {
for (int j=0; j<26; j++) {
freqA[i+1][j] = freqA[i][j] + (a[i]-'a'==j);
freqB[i+1][j] = freqB[i][j] + (b[i]-'a'==j);
}
}
for (int i=0; i<q; i++) {
int l,r;
cin >> l >> r;
int total = 0;
// cout << l << " <- l and r -> " << r << "\n";
for (int alpha=0; alpha<26; alpha++) {
int freqAlphaA = freqA[r][alpha] - freqA[l-1][alpha];
int freqAlphaB = freqB[r][alpha] - freqB[l-1][alpha];
// cout << (char)(alpha+'a') << ": " << freqAlphaA << " " << freqAlphaB << "\n";
if (freqAlphaA < freqAlphaB) total += freqAlphaB - freqAlphaA;
}
cout << total << "\n";
}
}
int main() {
cin.sync_with_stdio(0); cin.tie(0);
int t; cin >> t; while (t--) solve();
}