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) |
|
|
|
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) |
|
|
|
Returns: -1
|
In this case, it's impossible to transform A into
B. |
|
|
2) |
|
|
|
3) |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
"ZYXWVUTSRQPONMLKJIHGFEDCBA"
|
|
Returns: 25
|
|
|
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.
#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!
分享到:
相关推荐
topcoder的数学类算法题目。一个整数被称为k-smooth当且仅当它的最大素因子不大于k,给定N和K,计算出1 - N中有多少个整数是k-smooth。1 , 1 <= K <= 1000.
SRM2Multi dumper for hsap
omron系列CPM1/CPM1A/CPM2A/CPM2C/SRM1(-V2) PLC编程手册pdf,omron系列CPM1/CPM1A/CPM2A/CPM2C/SRM1(-V2) PLC编程手册
SAP SRM 介绍
Driver HASP SRM emulator (x86)
多年SRM实施经验总结,对希望从事SRM实施或规划的同学们有帮助
srm后端JAVA 供应商平台管理 标准物资开票表 bus_standard_invoice_out增加freeze_quantity(冻结数量这一列)。 标准物资开票表 bus_standard_invoice_out的主键为{行项目、采购订单号、物料凭证}。 标准物资...
分块描述SRM系统的作用:寻源、协同和考核 涉及具体的业务用途,供前期规划作参考,可根据实际情况调整,再考虑如何实现
ASP SRM USB Command Line Dumper Instructions. HASP SRM USB命令行转储指令。 WARNING!!! Before make dump from dongle make sure that you install the ...2. 2. Execute h7dmp.exe file. 执行h7dmp.exe文件。
简叙什么是SRM,SRM解决什么问题,SRM有用途,SRM功能等
SRM210 (PA)SAP SRM Server Configuration (Col92) Configuration
HASP_SRM_Runtime_setup
SRM Overview中文版让你更直观更容易了解SRM是什么,能做什么
SRM影像分割算法的matlab程序,主函数SRM_new
SRM空间富模型隐写分析算法,选区高维特征,使用集成分类器进行训练
不仅可以阅读srm格式文件,还可以制作文档。完全绿色破解,是一款不错的srm阅读器。
Workflow Guide SAP SRM 2007
HASP SRM加密狗简介,阿拉丁公司的各种加密够简介
不错的VMware SRM资料,可以看看
SRM基本功能介绍.pdf