nyoj-915-+-字符串
来源:互联网 发布:安知玉如意说的是什么 编辑:程序博客网 时间:2024/05/20 13:38
+-字符串
时间限制:1000 ms | 内存限制:65535 KB
难度:1
- 描述
- Shiva得到了两个只有加号和减号的字符串,字串长度相同。Shiva一次可以把一个加号和它相邻的减号交换。他想知道最少需要多少次操作才能把第一个字符串变换成第二个字符串。你现在要去帮助他完成那个这个问题。
- 输入
- 多组测试数据
每组数据有两行,每行包含一个由”+”和”-“最成的字符串。每个子符串长度不超过5000。 - 输出
- 仅一个整数,输出最少需要操作的次数。如果答案不存在,输出-1。
- 样例输入
++-+--+ -++--++
- 样例输出
4
- 来源
- NBOJ
- 上传者
- TC_周亿
/** * Project Name: nyoj_2.0 * File Name: 915.cpp * Created on: 2015年4月27日 下午7:55:24 * Author: jtahstu * QQ: 1373758426 E-mail:1373758426@qq.com * Copyright (c) 2015, jtahstu , All Rights Reserved. *///样例输入//++-+--+//-++--++//样例输出//4#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<string>#include<cstring>using namespace std;int main(){string a,b;while(cin>>a>>b){int count1=0,count2=0;for(unsigned int i=0;i<a.length();i++){if(a[i]=='+')count1++;if(b[i]=='+')count2++;}if(count1!=count2){cout<<"-1"<<endl;continue;}int sum=0;for(unsigned int i=0;i<a.length();i++){if(a[i]!=b[i]){for(unsigned int j=i+1;j<a.length();j++){if(a[j]==b[i]){swap(a[i],a[j]);sum+=j-i;break;}}}}cout<<sum<<endl;}return 0;}
0 0
- nyoj 915 +-字符串 【字符串】
- nyoj 915 +-字符串【字符串】
- nyoj-915-+-字符串
- NYOJ 915 +-字符串
- NYOJ 915 +-字符串
- NYOJ-915 +-字符串
- NYOJ 915 +-字符串
- NYOJ 915 +-字符串
- NYOJ 915 +-字符串【贪心】
- nyoj 915 +-字符串
- NYOJ 915 +-字符串
- NYOJ 题目915+-字符串
- nyoj-915-+-字符串
- NYOJ 915 +-字符串
- nyoj 915 + - 字符串 (递归)
- +-字符串 nyoj 915
- nyoj 915 +-字符串
- nyoj 915 +-字符串
- 开始
- android.database.sqlite.SQLiteException: Failed to change locale for db '/storage/sdcard0/xx/xxx.db'
- gdb 如何调试release版本
- Android中跨进程通信传递Parcelable对象时出现android.os.BadParcelableException: ClassNotFoundException when unmarsh
- hdu 1870 愚人节的礼物
- nyoj-915-+-字符串
- EMU8086 简单汇编程序&实现5个BYTES的数据相加
- (行为型模式九)备忘录模式
- C++中new、delete 与new[]、delete[]
- win8下安装pip
- 欢迎使用CSDN-markdown编辑器
- 单链表模板
- coco2dx-动作(Action)原理二 动作分类详解
- POJ1463 Strategic game(树形DP)