ccf-201403-3 命令行选项

ccf-201403-3 命令行选项

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct cmd {
    char a;
    string b,c;
};

void split(const string& s,vector<string>& v, const string& c) {
    string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(string::npos != pos2) {
        v.push_back(s.substr(pos1, pos2-pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length()) {
        v.push_back(s.substr(pos1));
    }
}

int find(vector<cmd> &vcmd,char a) {
    int i;
    for(i=0; i<vcmd.size(); i++) {
        if(vcmd[i].a==a) {
            if(vcmd[i].b=="!") {
                vcmd[i].c="1";
                return 1;
            }
            return 2;
        }
    }
    if(i==vcmd.size()) {
        return 0;
    }
}

void change(vector<cmd> &vcmd,char a,string b) {
    int i;
    for(i=0; i<vcmd.size(); i++) {
        if(vcmd[i].a==a) {
            vcmd[i].c=b;
        }
    }
}

bool com(cmd a,cmd b) {
    return a.a<b.a;
}

int main() {
    string scmd,temp;
    vector<cmd> vcmd;
    vector<string> vtemp;
    cmd t;
    cin>>scmd;
    for(int i=0; i<scmd.size(); i++) {
        if(scmd[i]!=':') {
            t.a=scmd[i];
            t.c=t.b="!";
            vcmd.push_back(t);
        } else {
            vcmd[vcmd.size()-1].c=vcmd[vcmd.size()-1].b="?";
        }
    }
    sort(vcmd.begin(),vcmd.begin()+vcmd.size(),com);
//  for(int i=0; i<vcmd.size(); i++) {
//      cout<<vcmd[i].a<<" "<<vcmd[i].b<<" "<<vcmd[i].c<<endl;
//  }
    int n;
    cin>>n;
    getline(cin,temp);
    for(int i=0; i<n; i++) {
        getline(cin,temp);
        split(temp,vtemp," ");
//      for(int j=0; j<vtemp.size(); j++) {
//          cout<<vtemp[j]<<" ";
//      }
//      cout<<endl;
        for(int j=1; j<vtemp.size(); j++) {
            if(vtemp[j][0]=='-'&&find(vcmd,vtemp[j][1])==1) {
            } else if(vtemp[j][0]=='-'&&find(vcmd,vtemp[j][1])==2) {
                if(j+1<vtemp.size()) {
                    change(vcmd,vtemp[j][1],vtemp[j+1]);
                    j++;
                } else {
                    break;
                }
            } else if(vtemp[j][0]=='-'&&find(vcmd,vtemp[j][1])==0) {
                break;
            } else if(vtemp[j][0]!='-') {
                break;
            }
        }
//      for(int j=0; j<vcmd.size(); j++) {
//          cout<<vcmd[j].a<<" "<<vcmd[j].b<<" "<<vcmd[j].c<<endl;
//      }
        vtemp.clear();
        cout<<"Case "<<i+1<<":";
        for(int j=0; j<vcmd.size(); j++) {
            if(vcmd[j].b=="!"&&vcmd[j].c!="!") {
                cout<<" -"<<vcmd[j].a;
            } else if(vcmd[j].b=="?"&&vcmd[j].c!="?") {
                cout<<" -"<<vcmd[j].a<<" "<<vcmd[j].c;
            }
        }
        cout<<endl;
        for(int j=0; j<vcmd.size(); j++) {
            vcmd[j].c=vcmd[j].b;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36792042/article/details/82429598