SRM 597 DIV2 500


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.


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class LittleElephantAndString {
public:
	int getNumber(string, string);
};

int LittleElephantAndString::getNumber(string A, string B)
{
    map<char,int> ma,mb;
    int len=A.size();
    for(int i=0;i<len;i++)
    {
        ma[A[i]]++;
        mb[B[i]]++;
    }
    bool flag=true;
    for(int i=0;i<len;i++)
    {
        if(ma[A[i]]==mb[A[i]]) continue;
        else {flag=false;break;}
    }
    if(flag==false) return -1;
    int i=len-1,j=len-1,ans=0;
    for(;i>=0&&j>=0;)
    {
        if(A[i]==B[j])
        {
            i--; j--;
        }
        else
        {
            i--; ans++;
        }
    }
    return ans;
}

///<%:testing-code%>
//Powered by [KawigiEdit] 2.0!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值