【CF1800E2】Unforgivable Curse (hard version)

文章讨论了一种编程问题,涉及将一个字符串转换成另一个字符串,允许通过交换字符,但限制在特定距离或加1的距离内。解题关键在于确定是否可以通过这些操作使两个字符串匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

洛谷链接

Unforgivable Curse (hard version)

题面翻译

与 E1 的差异仅在 k k k 的取值不同。

给定两个长度为 n n n 的字符串 s 1 , s 2 s_1,s_2 s1,s2 以及一个正整数 k k k,你可以进行任意次操作(可以不操作):

∣ i − j ∣ = k |i-j|=k ij=k ∣ i − j ∣ = k + 1 |i-j|=k+1 ij=k+1,交换 s 2 s_2 s2 i i i 个位置的字符与第 j j j 个位置的字符。

问最终能否使得 s 1 , s 2 s_1,s_2 s1,s2 完全相同。

本题有多组测试数据。

1 ≤ T ≤ 1 0 4 , 1 ≤ n , k ≤ 2 × 1 0 5 , ∑ n ≤ 2 × 1 0 5 1\leq T\leq 10^4,1\leq n,k\leq 2\times 10^5,\sum n\leq 2\times 10^5 1T104,1n,k2×105,n2×105

translated by @StayAlone

题目描述

This is a complex version of the problem. This version has no additional restrictions on the number $ k $ .

The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell $ s $ from his student Harry.

The spell — is a $ n $ -length string of lowercase Latin letters.

Drahyrt wants to replace spell with an unforgivable curse — string $ t $ .

Dragirt, using ancient magic, can swap letters at a distance $ k $ or $ k+1 $ in spell as many times as he wants. In other words, Drahyrt can change letters in positions $ i $ and $ j $ in spell $ s $ if $ |i-j|=k $ or $ |i-j|=k+1 $ .

For example, if $ k = 3, s = $ “talant” and $ t = $ “atltna”, Drahyrt can act as follows:

  • swap the letters at positions $ 1 $ and $ 4 $ to get spell “aaltnt”.
  • swap the letters at positions $ 2 $ and $ 6 $ to get spell “atltna”.

You are given spells $ s $ and $ t $ . Can Drahyrt change spell $ s $ to $ t $ ?

输入格式

The first line of input gives a single integer $ T $ ( $ 1 \le T \le 10^4 $ ) — the number of test cases in the test.

Descriptions of the test cases are follow.

The first line contains two integers $ n, k $ ( $ 1 \le n \le 2 \cdot 10^5 $ , $ 1 \le k \le 2 \cdot 10^5 $ ) — the length spells and the number $ k $ such that Drahyrt can change letters in a spell at a distance $ k $ or $ k+1 $ .

The second line gives spell $ s $ — a string of length $ n $ consisting of lowercase Latin letters.

The third line gives spell $ t $ — a string of length $ n $ consisting of lowercase Latin letters.

It is guaranteed that the sum of $ n $ values over all test cases does not exceed $ 2 \cdot 10^5 $ . Note that there is no limit on the sum of $ k $ values over all test cases.

输出格式

For each test case, output on a separate line “YES” if Drahyrt can change spell $ s $ to $ t $ and “NO” otherwise.

You can output the answer in any case (for example, lines “yEs”, “yes”, “Yes” and “YES” will be recognized as positive answer).

样例 #1

样例输入 #1

7
6 3
talant
atltna
7 1
abacaba
aaaabbc
12 6
abracadabraa
avadakedavra
5 3
accio
cicao
5 4
lumos
molus
4 3
uwjt
twju
4 3
kvpx
vxpk

样例输出 #1

YES
YES
NO
YES
NO
YES
NO

提示

The first case is explained in the condition.

In the second case, we can swap adjacent letters, so we can sort the string using bubble sorting, for example.

In the third case, we can show that from the string $ s $ we cannot get the string $ t $ by swapping letters at a distance of $ 6 $ or $ 7 $ .

In the fourth case, for example, the following sequence of transformations is appropriate:

  • “accio” $ \rightarrow $ “aocic” $ \rightarrow $ “cocia” $ \rightarrow $ “iocca” $ \rightarrow $ “aocci” $ \rightarrow $ “aicco” $ \rightarrow $ “cicao”

In the fifth case, we can show that it is impossible to get the string $ s $ from the string $ t $ .

In the sixth example, it is enough to swap the two outermost letters.

核心思路

这是一道思维题,不考虑越界的情况下,字符可以移动到任意一个位置。

如图
在这里插入图片描述
但是有可能会有越界,不过,越界十分好处理,对于字符串中的一个位置只要保证它可以往一边移动,那他就可以通过向另一边移动(如一个位置不能往右,但是可以往左走。说明这个位置太右了,它往左移动就可以让它没有那么右。),来达到两边都可以走的目的。

那么,只有两边都越界的才会无法移动。

所以,只需保证无法移动的字符在原串中相同即可。

AC代码

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a[101],b[101];
bool pd(int n){
	for(int i = 0;i < 26;i++){
		if(a[i] != b[i])return 0;
	}
	return 1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int n,k;
		cin>>n>>k;
		cin>>s1>>s2;
		for(int i = 0;i < n;i++){
				a[s1[i]-'a']++;
				b[s2[i]-'a']++;
			}
		//cout<<s1<<" "<<s2<<endl;
		if(k > n&&s1 != s2)cout<<"NO"<<endl;
		else if(!pd(n))cout<<"NO"<<endl;
		else {
			bool f = 1;
			for(int i = 0;i < n;i++){
				if(s1[i] != s2[i]&&i+k >= n&&i-k < 0&&f == 1){
					cout<<"NO"<<endl;
					f=0;
				}
			}
			if(f)cout<<"YES"<<endl;
		}
	}
}

结尾寄语

今刻沧桑登舞榭,万灵且待命无缰。

原图:B站夜呀呀呀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值