B.Scale
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<string> arr(n);
//依次读入每一行可以不用二维数组
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// 输出每个 k x k 块的左上角单元格的值
for (int i = 0; i < n; i += k) {
for (int j = 0; j < n; j += k)
cout << arr[i][j];
cout << endl;
}
}
return 0;
}
C.Sort
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
string a, b;
cin >> a >> b;
// 创建前缀和数组
vector<vector<int>> prefixA(n + 1, vector<int>(26, 0));
vector<vector<int>> prefixB(n + 1, vector<int>(26, 0));
// 计算前缀和
for (int i = 0; i < n; i++) {
for (int j = 0; j < 26; j++) {
prefixA[i + 1][j] = prefixA[i][j];
prefixB[i + 1][j] = prefixB[i][j];
}
prefixA[i + 1][a[i] - 'a']++;
prefixB[i + 1][b[i] - 'a']++;
}
// 处理查询
while (q--) {
int l, r;
cin >> l >> r;
// 计算子串a[l..r]和b[l..r]中每个字符的频率
int operations = 0;
for (int i = 0; i < 26; i++) {
int countA = prefixA[r][i] - prefixA[l - 1][i];
int countB = prefixB[r][i] - prefixB[l - 1][i];
operations += abs(countA - countB);
}
cout << operations / 2 << endl;
}
}
return 0;
}
·····················································································