zoukankan      html  css  js  c++  java
  • ACM +-字符串

    +-字符串

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:1
     
    描述
    Shiva得到了两个只有加号和减号的字符串,字串长度相同。Shiva一次可以把一个加号和它相邻的减号交换。他想知道最少需要多少次操作才能把第一个字符串变换成第二个字符串。你现在要去帮助他完成那个这个问题。
     
    输入
    多组测试数据

    每组数据有两行,每行包含一个由”+”和”-“最成的字符串。每个子符串长度不超过5000。
    输出
    仅一个整数,输出最少需要操作的次数。如果答案不存在,输出-1。
    样例输入
    ++-+--+ 
    -++--++ 
    样例输出
    4

    本题的思路是统计两字符串个+的位置,然后将第一个字符串的第i个+移到第二个字符串的第i个+的位置,将两个字符串的对应+的索引相减即为移动的距离
    此题要注意
        while(cin >> a >>b)多组测试数据
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstdlib>
    using namespace std;
    
    int main(){
        string a,b;
        while(cin >> a >>b){
            vector<int> plusA,plusB;
            for(int i = 0 ; i < a.length(); ++ i){
                if(a[i] == '+') plusA.push_back(i);
                if(b[i] == '+') plusB.push_back(i);
            }
            int dist = 0;
            for(int i = 0 ; i < plusA.size(); ++i)
                dist += abs(plusA[i]-plusB[i]);
            cout<<dist<<endl;
        }
    }
    
    
    
     
  • 相关阅读:
    HTTP断点续传 规格严格
    Java Shutdown 规格严格
    linux 命令源码 规格严格
    JTable调整列宽 规格严格
    linux 多CPU 规格严格
    Hello can not find git path 规格严格
    Kill 规格严格
    拜拜牛人 规格严格
    Swing 规格严格
    Debugging hangs in JVM (on AIX but methodology applicable to other platforms) 规格严格
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3654217.html
Copyright © 2011-2022 走看看