標籤:
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 DescriptionIn 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.
InputThere 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.
OutputFor 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 Input3abbabacddbaaabbcbccd
Sample OutputCase #1: NOCase #2: YESCase #3: YESHintFor 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[email protected]_Goldfinger
Source2012 Multi-University Training Contest 6 題目大意:給你兩個串s和t。讓你判斷是否可以從s轉化到t。轉化規則是你可以無限次交換任意兩個字元,然後兩個字元的值會+1。相應變化a -> b 、 b -> c 、z -> a。 解題思路:轉自 http://blog.sina.com.cn/s/blog_ab8d947001017e8h.html
把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有沒有變形成功即可
HDU 4357——String change——————【規律題】