HDU 4357——String change——————【规律题】

本文探讨了如何通过字符交换操作将一个字符串转化为另一个字符串,包括字符价值递增的规则和实现方法,以及如何判断两个字符串是否可以通过有限次数的操作相互转换。

String change

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1116    Accepted Submission(s): 494


Problem Description
In this problem you will receive two strings S 1 and S 2 that contain only lowercase letters.
Each time you can swap any two characters of S 1. After swap,both of the two letters will increase their value by one. If the previous letter is 'z',it will become 'a' after being swapped.
That is to say ,"a" becomes "b","b" becomes "c"....."z" becomes "a" and so on.
You can do the change operation in S 1 as many times as you want.
Please tell us whether you can change S 1 to S 2 after some operations or not.
 

 

Input
There are several cases.The first line of the input is a single integer T (T <= 41) which is the number of test cases.Then comes the T test cases .

For each case,the first line is S 1,the second line is S 2.S 1 has the same length as S 2 and the length of the string is between 2 and 60.
 

 

Output
For each case,output "Case #X: " first, X is the case number starting from 1.If it is possible change S 1 to S 2 output "YES",otherwise output "NO".
 

 

Sample Input
3
ab ba
bac ddb
aaabb cbccd
 

 

Sample Output
Case #1: NO
Case #2: YES
Case #3: YES
Hint
For the first case,it's impossible to change "ab" to "ba" . For the second case,swap(S1[0],S1[2])->swap(S1[1],S1[2]),meanwhile:bac->dac->ddb. For the third case,swap(S1[0],S1[3])->swap(S1[1],S1[2])->swap(S1[2],S1[3])->swap(S1[3],S1[4]), meanwhile:aaabb->caabb->cbbbb->cbccb->cbccd.
 

 

Author
miketc@UESTC_Goldfinger
 

 

Source
 
 
题目大意:给你两个串s和t。让你判断是否可以从s转化到t。转化规则是你可以无限次交换 任意两个字符,然后两个字符的值会+1。相应变化a -> b 、 b -> c 、z -> a。
 

把26个字母看成0~25对应的数字,当数慢慢增大时就对26取模,则字符串有一个总和s1,要使其变为末状态的总和s2;那么每交换一次s1要加2,故,s1+s2必须为偶数。

两个字母单独处理,两个以上时,以三个数字为例,(a,b,c)为三个数,则有(a,b,c)->(a,c+1,b+1)->(c+2,,a+1,b+1)->(c再分别和a,b各交换12次,有(c+26,a+13,b+13),再a,b相互交换13次得(c+26,b+26,a+26);而26为一个周期,即39次交换后由(a,b,c)->(c,b,a)(中间不动,两边交换了);同理可证39次交换后,可由(a,b,c)->(b,a,c)(一边不动,相邻的交换);因此,一开始可任意按需要进行交换使s1中的各数与s2中的各数相等,再进行调位置。如果是两个数就不一样了,设要由(a1,a2)->(b1,b2),则a1必经偶数变为b1或经奇数步变为b2;又由于26步之后a1,a2又会变回原状态,故必在26步之内要解决变形,先设a1变形成功,再只须检查a2有没有变形成功即可

 

转载于:https://www.cnblogs.com/chengsheng/p/4768139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值