+-字符串
来源:互联网 发布:编程方法学 编辑:程序博客网 时间:2024/06/10 15:18
- 输入
- 多组测试数据
每组数据有两行,每行包含一个由”+”和”-“最成的字符串。每个子符串长度不超过5000。 - 输出
- 仅一个整数,输出最少需要操作的次数。如果答案不存在,输出-1。
- 样例输入
++-+--+ -++--++
- 样例输出
4
思路:
将a字串变成b字串,首先判断字符串长度和+号是否相等,题目要求移动次数,按照下标对比两个字符串的元素,
代码:
#include<iostream>#include<algorithm>#include<cstring>#include<stdio.h>using namespace std;int main(){char a[50001],b[50001];int len,lena,lenb,i,j,c=0;while (scanf("%s%s",a,b)!=EOF){lena=c=lenb=0;len = strlen(a); //字符串相等,求一个即可 for (i=0;i<len;i++){if (a[i]=='+') lena++;if (b[i]=='+') lenb++;}if (lena != lenb) continue; //+号数目不等for (i=0;i<len;i++){if (a[i] != b[i])//同下标却不相等的需要移动 {for (j=i+1;j<len;j++){c++;if (a[j]!=a[i]){a[j]=a[i];break;}}} } cout<<c<<endl;}return 0;}
0 0
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 【2017最新完整】超星尔雅通识课木与人类文明答案
- DBA_HIST_SYSMETRIC_SUMMARY中的Network Traffic Volume Per Sec 指标是指什么?
- spring---整合Junit,整合web
- caffe-windows(仅cpu)配置过程
- 除夕和春节
- +-字符串
- usaco street race(dfs)
- leetcode 3. Longest Substring Without Repeating Characters
- 删除右键新建多余菜单
- vue-class绑定
- hdu 1599 Common Subsequence(DP)
- 孩子们的游戏(圆圈中最后剩下的数)(约瑟夫环问题)
- linux给用户添加sudo权限
- ios swift 图形的旋转 atan2 函数