Topcoder SRM 597 div2 B
Problem Statement |
|||||||||||||
|
Little Elephant from the Zoo of Lviv likes strings.
You are given a string A and a string B of the same length. In one turn Little Elephant can choose any character of A and move it to the beginning of the string (i.e., before the first character of A). Return the minimal number of turns needed to transform A into B. If it's impossible, return -1 instead. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
| - | A will contain between 1 and 50 characters, inclusive. | ||||||||||||
| - | B will contain between 1 and 50 characters, inclusive. | ||||||||||||
| - | A and B will be of the same length. | ||||||||||||
| - | A and B will consist of uppercase letters ('A'-'Z') only. | ||||||||||||
Examples |
|||||||||||||
| 0) | |||||||||||||
|
|||||||||||||
| 1) | |||||||||||||
|
|||||||||||||
| 2) | |||||||||||||
|
|||||||||||||
| 3) | |||||||||||||
|
|||||||||||||
| 4) | |||||||||||||
|
|||||||||||||
| 5) | |||||||||||||
|
|||||||||||||
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
1 #include <string> 2 #include <algorithm> 3 using namespace std; 4 class LittleElephantAndString{ 5 public: 6 int getNumber(string A, string B){ 7 string a=A,b=B; 8 sort(a.begin(),a.end()); 9 sort(b.begin(),b.end()); 10 if(a!=b)return -1; 11 int ans=0; 12 for(int i=A.length()-1,x=B.length()-1;i>=0;i--,x--) 13 while(x>=0&&B[i]!=A[x])x--,ans++; 14 return ans; 15 } 16 };
浙公网安备 33010602011771号