codeforces 962(div.3)的B题和C题

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;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值