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 S1 and S2 that contain only lowercase letters.
Each time you can swap any two characters of S1. 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 S1 as many times as you want.
Please tell us whether you can change S1 to S2 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 S1,the second line is S2.S1 has the same length as S2 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 S1 to S2 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