962(div.3)B-Scale
没啥思路,就纯模拟
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n';
const int N = 1e3 + 11;
int a[N][N];
int b[N][N];
signed main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
string s; //由于输入没有空格,所以用字符串读取
cin >> s;
for (int l = 0; l < s.size(); l++)
{
a[i][l] = s[l] - 48;
}
}
int x = k;
for (int i = 0; i < n/k; i++) //循环n/k*n/k次,每次输出k*k块中右下角的那一个数就行
{
int y = k;
for (int j = 0; j < n/k; j++)
{
cout << a[x - 1][y - 1];
y += k;
}
x += k;
cout << endl;
}
}
return 0;
}
962(div.3)C. Sort
因为排序后是固定的,所以思路就是统计l~r范围内两个字符串相同的字符有几个,然后长度减去相同的字符就是最终答案。
注意:清零时,不能用memset(),会超时
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int N = 2e5 + 11;
int a[N][30];
int b[N][30];
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n, q;
cin >> n >> q;
string A, B;
cin >> A >> B;
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < 26; j++) //清零,不能用memset(),会超时
{
a[i][j] = b[i][j] = 0;
}
}
for (int i = 1; i <= n; i++)
{
a[i][A[i - 1] - 'a']++; //遍历字符串,统计在i长度内,该字符出现的次数
b[i][B[i - 1] - 'a']++;
}
for (int j = 0; j < 26; j++) //遍历26个字符,两两相加,最后就是A[I][J]代表在长度为I内,J字符出现的次数
for (int i = 1; i <= n; i++)
{
a[i][j] += a[i - 1][j];
b[i][j] += b[i - 1][j];
}
while (q--)
{
int l, r;
cin >> l >> r;
int ans = 0; //ans记录的是在l~r范围内,两个字符串相同的字符的数量
for (int j = 0; j < 26; j++)
{
ans += min(a[r][j] - a[l - 1][j], b[r][j] - b[l - 1][j]); //去最小,及共有的有几个
}
ans = r - l + 1 - ans; //最后答案就是长度减去相同的数量,及要修改的次数
cout << ans << endl;
}
}
return 0;
}