• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

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          

       
                                                                                                                                                                                                                                                                                                                                                    
                Class:                               LittleElephantAndString              
                Method:                               getNumber              
                Parameters:                               string, string              
                Returns:                               int              
                Method signature:                               int getNumber(string A, string B)              
                (be sure your method is public)              
       
                      
                 
         

            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)                          
                                                                                                                                                            
                                                                                                                                               
                     
"ABC"
                   
                     
"CBA"
                   
             
               
Returns: 2
             
                                                                                       
                      The optimal solution is to make two turns. On the first                       turn, choose character 'B' and obtain string "BAC". On                       the second turn, choose character 'C' and obtain "CBA".                    
             
       
          1)                          
                                                                                                                                                            
                                                                                                                                               
                     
"A"
                   
                     
"B"
                   
             
               
Returns: -1
             
                                                                                       
                      In this case, it's impossible to transform A into B.                    
             
       
          2)                          
                                                                                                                                                            
                                                                                                                                               
                     
"AAABBB"
                   
                     
"BBBAAA"
                   
             
               
Returns: 3
             
                                                                                       
                                         
             
       
          3)                          
                                                                                                                                                            
                                                                                                                                               
                     
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                   
                     
"ZYXWVUTSRQPONMLKJIHGFEDCBA"
                   
             
               
Returns: 25
             
                                                                                       
                                         
             
       
          4)                          
                                                                                                                                                            
                                                                                                                                               
                     
"A"
                   
                     
"A"
                   
             
               
Returns: 0
             
                                                                                       
                                         
             
       
          5)                          
                                                                                                                                                            
                                                                                                                                               
                     
"DCABA"
                   
                     
"DACBA"
                   
             
               
Returns: 2
             
                                                                                       
                                         
             
       

   

      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 };
View Code
posted @ 2013-11-21 21:22  HaibaraAi  阅读(93)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3