闂鏉ヨ嚜 TopCoder SRM 591 DIV 2 鐨勭浜岄錛?br />
Problem Statement
Let X and Y be two strings of equal length, consisting of uppercase English letters only. The two strings are called convertible if there is a permutation P of the English alphabet with the following property: if each character c in the string X is replaced by the character P(c), we obtain the string Y. (In other words, X and Y are convertible iff the following holds: whenever two letters of X are equal, the corresponding two letters of Y must be equal, and vice versa.) For example, consider the string "ABCA". We can choose to replace each 'A' by a 'F', each 'B' by a 'B', and each 'C' by a 'G', obtaining the string "FBGF". Thus the strings "ABCA" and "FBGF" are convertible. The strings "ABCA" and "EFGH" are not convertible, because the two 'A's in the first string must correspond to the same letter in the second string. The strings "ABCA" and "EEEE" are not convertible, because different letters in the first string must correspond to different letters in the second string. You are given two strings A and B of the same length. These strings only contain English letters from 'A' to 'I', inclusive. (That is, only the first 9 letters of the alphabet are used.) You want to change A and B into two strings that are convertible. The only allowed change is to choose some indices (possibly none) and to remove the characters at those indices from each of the strings. (I.e., the removed characters must be at the same positions in both strings. For example, it is not allowed to only remove character 0 of A and character 3 of B.) For example, if A="ABAC", B="AHHA" and the chosen indices are 0 and 2, then the resulting strings will be "BC" and "HA". Our goal is to choose as few indices as possible, given that after the erasing we want to obtain two convertible strings. Compute and return the smallest possible number of chosen indices.
Definition
Class:
ConvertibleStrings
Method:
leastRemovals
Parameters:
string, string
Returns:
int
Method signature:
int leastRemovals(string A, string B)
(be sure your method is public)
Constraints
-
A will contain between 1 and 50 characters, inclusive.
-
A and B will be of the same length.
-
A will contain characters from 'A' to 'I' only.
-
B will contain characters from 'A' to 'I' only.
鎴戠殑鎬濊礬鏄┓涓続涓瓧姣嶄笌B涓瓧姣嶇殑瀵瑰簲鍏崇郴錛岀湅鍝瀵瑰簲闇瑕佸垹闄ょ殑瀛楁瘝鏈灝戯紝榪欎竴鏈灝戝煎嵆鏄瓟妗堛?br />絀蜂婦瀵瑰簲鍏崇郴錛屽氨鏄敓鎴愬叏鎺掑垪銆?br />鎴戠敓鎴愬叏鎺掑垪鐨勬柟寮忔槸鍥炴函銆?br />
涔嬪悗鐪嬪叾浠栦漢鐨勪唬鐮侊紝鍙戠幇涓涓敱緇欏畾鎺掑垪姹傚嚭鍏朵笅涓涓帓鍒楃殑鍑芥暟錛屼簬鏄涔?fàn)涓涓嬶紝鑷繁瀹炵幇濡備笅錛?br />
// 鐢熸垚涓嬩竴瀛楀吀搴忔帓鍒?br />// 鍋囪a涓厓绱犱簰涓嶇浉鍚?br />// 鑻ュ凡緇忔槸鏈鍚庝竴涓瓧鍏稿簭鎺掑垪錛屽垯榪斿洖0錛屽惁鍒欒繑鍥?
int next_permutation( int a[], int n ) {
int i, j;
for ( i = n-1; (0 < i) && (a[i-1] > a[i]); --i ) {
}
if ( 0 >= i ) {
return 0;
}
for ( j = n-1; j >= i; --j ) {
if ( a[ j ] > a[ i-1 ] ) {
int tmp = a[ i-1 ];
a[ i-1 ] = a[ j ];
a[ j ] = tmp;
j = n - 1;
while ( i < j ) {
tmp = a[ i ];
a[ i ] = a[ j ];
a[ j ] = tmp;
++i; --j;
}
break;
}
}
return 1;
}
榪樻湁浜轟嬌鐢ㄧ殑鏄疌錛嬶紜鐨?<algorithm> 涓?next_permutation 鍑芥暟錛屽姛鑳戒竴鏍楓?br />

]]>