1451C - String Equality

本文探讨了通过特定操作将一个字符串转化为另一个字符串的问题。首先允许字符间的任意交换,其次可以通过连续K个字符递增来改变字符串。文章提供了一种验证转换是否可行的方法,并附带实现代码。

传送门

题意:给出两个字符串a,b,用以下操作:<1>可以交换任意两个相邻字符的位置。<2>可以将连续的K个字符加1,如k=3,则bbbaa,可变为cccaa。最后能将a变为b。

思路:
对于第一个操作的运用:看到可以交换任意两个相邻字符位置,其实等同于可以给所有字符重新排列,如此我们需要的只是把a的各个字符相应的数量转变为b的各个字符数量即可。

对于第二操作的应用:可以将相邻的K个字符加一,那么我们为了更好的运用,就直接把所有的相同字符排列在一起,然后按字符从‘a’到’z’比较,如果a某低位的字符加上之前剩余可转化字符大于等于b的则可以完成b的相对应的某字符转化,且必须将剩余的某字符转化成下一个字符,这样才可以达到a b对于这个字符数量相等,直到每个字符都可以达成转化则对,具体见代码。

    #include<bits/stdc++.h>
    #define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    using namespace std;
    typedef long long ll;
    int sum1[30], sum2[30];
    int main()
    {
    	FAST;
    	int t;cin >> t;
    	while (t--)
    	{
    		int n, k;cin >> n >> k;
    		string str1, str2;cin >> str1 >> str2;
    		memset(sum1, 0, sizeof(sum1));
    		memset(sum2, 0, sizeof(sum2));
    		for (int i = 0;i < str1.size();i++)
    		{
    			sum1[str1[i] - 'a' + 1]++;
    			sum2[str2[i] - 'a' + 1]++;
    		}
    		int f = 1, sum = 0;
    		for (int i = 1;i <= 26;i++)
    		{
    			if (sum1[i]+sum < sum2[i])
    			{
    				f = 0;break;
    			}
    			sum += (sum1[i] - sum2[i]);
    			if (sum % k != 0)//不能整除
    			{
    				f = 0;break;
    			}
    		}
    		if (f)cout << "Yes" << endl;
    		else cout << "No" << endl;
    	}
    }
git config usage: git config [<options>] Config file location --[no-]global use global config file --[no-]system use system config file --[no-]local use repository config file --[no-]worktree use per-worktree config file -f, --[no-]file <file> use given config file --[no-]blob <blob-id> read config from given blob object Action --[no-]get get value: name [value-pattern] --[no-]get-all get all values: key [value-pattern] --[no-]get-regexp get values for regexp: name-regex [value-pattern] --[no-]get-urlmatch get value specific for the URL: section[.var] URL --[no-]replace-all replace all matching variables: name value [value-pattern] --[no-]add add a new variable: name value --[no-]unset remove a variable: name [value-pattern] --[no-]unset-all remove all matches: name [value-pattern] --[no-]rename-section rename section: old-name new-name --[no-]remove-section remove a section: name -l, --[no-]list list all --[no-]fixed-value use string equality when comparing values to 'value-pattern' -e, --[no-]edit open an editor --[no-]get-color find the color configured: slot [default] --[no-]get-colorbool find the color setting: slot [stdout-is-tty] Type -t, --[no-]type <type> value is given this type --bool value is "true" or "false" --int value is decimal number --bool-or-int value is --bool or --int --bool-or-str value is --bool or string --path value is a path (file or directory name) --expiry-date value is an expiry date Other -z, --[no-]null terminate values with NUL byte --[no-]name-only show variable names only --[no-]includes respect include directives on lookup --[no-]show-origin show origin of config (file, standard input, blob, command line) --[no-]show-scope show scope of config (worktree, local, global, system, command) --[no-]default <value> with --get, use default value when missing entry --[no-]comment <value> human-readable comment string (# will be prepended as needed) 这样意味着设置好用户名和电子邮箱了吗
最新发布
10-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Rikka_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值