日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Anagram

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Anagram 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 65536K,其他語言131072K
64bit IO Format: %lld

題目描述?

Orz has two strings of the same length:?A?and?B. Now she wants to transform?A?into an anagram of?B?(which means, a rearrangement of?B) by changing some of its letters. The only operation the girl can make is to “increase” some (possibly none or all) characters in?A. E.g., she can change an ‘A’ to a ‘B’, or a ‘K’ to an ‘L’. She can increase any character any times. E.g., she can increment an ‘A’ three times to get a ‘D’. The increment is cyclic: if she increases a ‘Z’, she gets an ‘A’ again.

????For example, she can transform “ELLY” to “KRIS” character by character by shifting ‘E’ to ‘K’ (6 operations), ‘L’ to ‘R’ (again 6 operations), the second ‘L’ to ‘I’ (23 operations, going from ‘Z’ to ‘A’ on the 15-th operation), and finally ‘Y’ to ‘S’ (20 operations, again cyclically going from ‘Z’ to ‘A’ on the 2-nd operation). The total number of operations would be?6 + 6 + 23 + 20 = 55. However, to make “ELLY” an anagram of “KRIS” it would be better to change it to “IRSK” with only 29 operations. You are given the strings?A?and?B. Find the minimal number of operations needed to transform A into some other string?X, such that?X?is an anagram of?B.

輸入描述:

There will be multiple test cases. For each testcase:There is two strings A and B in one line.∣A∣=∣B∣≤50. A and B will contain only uppercase letters from the English alphabet (‘A’-‘Z’).

輸出描述:

For each test case, output the minimal number of operations.示例1

輸入

ABCA BACA ELLY KRIS AAAA ZZZZ

輸出

0 29 100

題解:對輸入的兩個字符串先排序,如果a字符<=b的字符,改變的字符次數就是大的減小的,把改變成的字符,b標記為*表示改變了,繼續走,如果a>b字符,就用a-b+26,因為是環,所以加26。

#include<bits/stdc++.h> using namespace std; int main() {char a[100],b[100];int ans;while(~scanf("%s %s",a,b)){ans=0;int len=strlen(a);sort(a,a+len);sort(b,b+len);for(int i=0; i<len; i++)for(int j=0; j<len; j++){if(a[i]<=b[j]&b[j]!='*'){ans+=b[j]-a[i],b[j]='*';break;}if(j==len-1){for(int k=0; k<len; k++){if(b[k]!='*'){ans+=26-(a[i]-b[k]),b[k]='*';break;}}}}cout<<ans<<endl;}return 0; }

總結

以上是生活随笔為你收集整理的Anagram的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。