其他分享
首页 > 其他分享> > Codeforces 1017C. String Equality(思维)

Codeforces 1017C. String Equality(思维)

作者:互联网

Decription

在这里插入图片描述
Example
input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc

output
No
Yes
No
Yes

Solution
注意到操作1本质上就是对字符串的任意重排,说明位置不需要考虑
操作2是不可逆的,即a->b 后无法实现b->a,我们记录下每个字母的个数顺序递推过去即可

Code

const int maxn = 2e6 + 10;
char a[maxn],b[maxn];
int num1[100], num2[100];
bool solve(int n,int k) {
	for(int i = 0;i < 26;++i) {
		if(num1[i]<num2[i]) return false;
		if(num1[i] == num2[i]) continue;
		if(num1[i]>num2[i]) {
			if(i == 25) return false;
			int d = num1[i] - num2[i];
			if(d%k!=0) return false;
			num1[i+1] += d;
		}
	}return true;
}
int main(int argc, char const *argv[])
{
	int T;scanf("%d",&T);
	while(T--) {
		int n,k;scanf("%d %d",&n,&k);
		scanf("%s %s",a+1,b+1);
		clr(num1,0);clr(num2,0);
		for(int i = 1;i <= n;++i) {
			int x1 = a[i] - 'a', x2 = b[i] - 'a';
			num1[x1]++, num2[x2]++;
		}
		bool res = solve(n,k);
		if(res) printf("Yes\n"); else printf("No\n");
	}
	return 0;
}

标签:Equality,num1,num2,No,int,Codeforces,++,return,1017C
来源: https://blog.csdn.net/qq_43521140/article/details/110123089