hdu3242List Operations(字符串相加减)

时间:2021-05-28 09:26:47

src http://hdu.hustoj.com/showproblem.php?pid=3242

用vector来做删除酱紫~~~神奇海螺????

ac代码:

#include<iostream>
using namespace std;
#include<stdio.h>
#include<string.h>
#include<string>
#include<cmath>
#include<vector>
#define FOR(i,a,b) for(int i=a;i<=b;i++)

string s1, s2, s3;
vector<string>vt;
vector<string>::iterator it;
void push(string str)
{
    int len = str.size();
    string s = "";
    for (int i = 1; i<len; i++) {
        if (str[i] == ',' || str[i] == ']') {
            if (s != "")vt.push_back(s);
            s = "";
        }
        else { s += str[i]; }
    }
}
void add(string str)
{
    int len = str.size();
    string s = "";
    for (int i = 1; i<len; i++) {
        if (str[i] == ',' || str[i] == ']') {
            if (s != "")vt.push_back(s);
            s = "";
        }
        else s += str[i];
    }
}
void sub(string str)
{
    int len = str.size();
    string s = "";
    for (int i = 1; i<len; i++) {
        if (str[i] == ',' || str[i] == ']') {
            for (it = vt.begin(); it != vt.end(); it++) {
                if (*it == s) { vt.erase(it); break; }
            }
            s = "";
        }
        else s += str[i];
    }
}
int main()
{
    while (cin >> s1) {
        vt.clear();
        if (s1 == ".")break;
        push(s1);
        cin >> s2 >> s3;
        if (s2 == "++") {
            add(s3);
        }
        else { sub(s3); }
        cout << '[';
        if (!vt.empty()) {
            it = vt.begin();
            cout << *it; it++;
            while (it != vt.end()) { cout << ',' << *it; it++; }
        }
        cout << ']' << endl;
    }
    return 0;
}