题解:Codeforces Round 962 (Div. 3) C

C. Sort

time limit per test: 5 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given two strings a a a and b b b of length n n n. Then, you are (forced against your will) to answer q q q queries.

For each query, you are given a range bounded by l l l and r r r. In one operation, you can choose an integer i i i ( l ≤ i ≤ r l \leq i \leq r lir) and set a i = x a_i = x ai=x where x x x is any character you desire. Output the minimum number of operations you must perform such that sorted(a[l..r]) = sorted(b[l..r]) \texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])} sorted(a[l..r])=sorted(b[l..r]). The operations you perform on one query does not affect other queries.

For an arbitrary string c c c, sorted(c[l..r]) \texttt{sorted(c[l..r])} sorted(c[l..r]) denotes the substring consisting of characters c l , c l + 1 , . . . , c r c_l, c_{l+1}, ... , c_r cl,cl+1,...,cr sorted in lexicographical order.

给你两个长度为 n n n 的字符串 a a a b b b 。然后,您(被迫)回答 q q q 个问题。

每个查询都会给你一个由 l l l r r r 限定的范围。在一次操作中,您可以选择一个整数 i i i ( l ≤ i ≤ r l \leq i \leq r lir ) 并设置 a i = x a_i = x ai=x 其中 x x x 是您想要的任何字符。输出必须执行的最少操作数,以便 sorted(a[l..r]) = sorted(b[l..r]) \texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])} sorted(a[l..r])=sorted(b[l..r]) 。**对一个查询执行的操作不会影响其他查询。

对于任意字符串 c c c sorted(c[l..r]) \texttt{sorted(c[l..r])} sorted(c[l..r]) 表示由按词典顺序排列的字符 c l , c l + 1 , . . . , c r c_l, c_{l+1}, ... , c_r cl,cl+1,...,cr 组成的子串。

Input

The first line contains t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) – the number of test cases.

The first line of each test case contains two integers n n n and q q q ( 1 ≤ n , q ≤ 2 ⋅ 1 0 5 1 \leq n, q \leq 2 \cdot 10^5 1n,q2105) – the length of both strings and the number of queries.

The following line contains a a a of length n n n. It is guaranteed a a a only contains lowercase latin letters.

The following line contains b b b of length n n n. It is guaranteed b b b only contains lowercase latin letters.

The following q q q lines contain two integers l l l and r r r ( 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1lrn) – the range of the query.

It is guaranteed the sum of n n n and q q q over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输入

第一行包含 t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000 ) - 测试用例数。

每个测试用例的第一行包含两个整数 n n n q q q ( 1 ≤ n , q ≤ 2 ⋅ 1 0 5 1 \leq n, q \leq 2 \cdot 10^5 1n,q2105 ) - 两个字符串的长度和查询次数。

下面一行包含长度为 n n n a a a 。可以保证 a a a 只包含小写拉丁字母。

下面一行包含长度为 n n n b b b 。保证 b b b 只包含小写拉丁字母。

下面的 q q q 行包含两个整数 l l l r r r ( 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1lrn ) - 查询范围。

保证所有测试用例中的 n n n q q q 之和不超过 2 ⋅ 1 0 5 2 \cdot 10^5 2105

Output

For each query, output an integer, the minimum number of operations you need to perform in a new line.

输出

对于每个查询,在新行中输出一个整数,即需要执行的最少操作数。

Example

Input
3
5 3
abcde
edcba
1 5
1 4
3 3
4 2
zzde
azbe
1 3
1 4
6 3
uwuwuw
wuwuwu
2 4
1 3
1 6
Output
0
1
0
2
2
1
1
0

Note

For the first query, sorted(a[1..5]) = \texttt{sorted(a[1..5])} = sorted(a[1..5])= abcde and sorted(b[1..5]) = \texttt{sorted(b[1..5])} = sorted(b[1..5])= abcde, so no operations are necessary.

For the second query, you need to set a 1 = a_1 = a1= e. Then, sorted(a[1..4]) = sorted(b[1..4]) = \texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = sorted(a[1..4])=sorted(b[1..4])= bcde.

对于第一个查询, sorted(a[1..5]) = \texttt{sorted(a[1..5])} = sorted(a[1..5])= abcde 和 sorted(b[1..5]) = \texttt{sorted(b[1..5])} = sorted(b[1..5])= abcde,因此无需进行任何操作。

对于第二个查询,需要设置 a 1 = a_1 = a1= e,然后是 sorted(a[1..4]) = sorted(b[1..4]) = \texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = sorted(a[1..4])=sorted(b[1..4])= bcde。

题意

每次给你两个数字 n , q n,q n,q ,两个长度为 n n n 的字符串 a , b a,b a,b
然后进行 q q q 次询问,每次询问给出两个数字 l , r l,r l,r
表示对字符串 a , b a,b a,b [ l , r ] [l,r] [l,r] 区间进行排序(排序后恢复原样,不影响下一次询问)
每次排序前可以对字符串进行操作:把第 i i i 个字符替换成任意字符。
问:每次排序前需要进行至少多少次操作,才能使字符串 a , b a,b a,b 排序完后的区间内容相同

题解

很显然,如果暴力做的话每次一定会超时
题目说的是排序,实际上是查重
在这里我们可以做一个预处理:前缀和
在每个字符的地方创建一个数组,记录每种字母前面出现次数

  • 假如在字符串 a a a 出现过,那么该字符对应的数组内的元素就 加1
  • 假如在字符串 b b b 出现过,那么该字符对应的数组内的元素就 减1

以此完成查重
然后每次询问,我们对 r r r l − 1 l-1 l1 内的数组进行处理
对每个下标(一共 26 26 26 个嘛),假如数组 r r r 内的元素 大于 数组 l − 1 l-1 l1 内的元素,就加上他们的差值(也就是要改变的数量)
然后输出就好了

代码

#include <bits/stdc++.h>

#define int long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()

using i64 = long long;

int t = 1;

void solve(){
    int n,q;
    std::cin >> n >> q;
    std::string a,b;
    std::cin >>a >> b;
    std::vector<std::array<int,26> > qzh(n+1);
    for(int i = 0 ; i < n ; i ++) {
        qzh[i+1] = qzh[i];
        qzh[i+1][a[i]-'a']++;
        qzh[i+1][b[i]-'a']--;
    }

    for(int i = 0 ; i < q ; i ++) {
        int l,f;
        std::cin >> l >> f;
        l--;
        int ans = 0;
        for(int j = 0 ; j < 26 ; j ++) {
            ans += std::max(qzh[f][j] - qzh[l][j],(int)0);
        }
        std::cout << ans << "\n";
    }

}

signed main(){
    std::cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18331670
博主已同意,我就是博主

  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值