[Liu rujiashu] example 5-11 UVA814 (string segmentation, weight determination, etc.)

[2019.4.5]
Note: there may be duplicate receiving address
I didn't think about this for the first time. I didn't think about it. Later, I read the code in the book and found that there was a heavy judgment

At first, I was going to number the string, and I felt like I was in a state of bewilderment
Deeply aware of the importance of data structure, it's really important to choose a good way to organize data
This copy is really frustrating. AC is really depressed in the morning

How to write a split string:

void cut(const string& s, string& name, string& city)
{
    int k = s.find('@');    //s[k]=='@'
    name = s.substr(0, k);  //0 to k, excluding k
    city = s.substr(k+1);   //k+1 to the end
}

All codes:

#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>

using namespace std;

set<string> addr;   //All legal addresses, name@city

map<string, vector<string>> dest;   //Receiving city vector < name >
vector<string> city;                //Send to receiving city in order

set<string> vis;        //Determine whether the receiving address is duplicate

void cut(const string& s, string& name, string& city)
{
    int k = s.find('@');    //s[k]=='@'
    name = s.substr(0, k);  //0 to k, excluding k
    city = s.substr(k+1);   //k+1 to the end
}

int main()
{
    //freopen("C:\\Users\\Summer\\Desktop\\input.txt", "r", stdin);
    //freopen("C:\\Users\\Summer\\Desktop\\output.txt", "w", stdout);

    string s, s1;
    int name_cnt;

    //Input MTA
    while(cin>>s && s!="*") {
        cin>>s;
        //cout<<s<<endl;

        cin>>name_cnt;
        //cout<<name_cnt<<endl;
        for(int i=0; i<name_cnt; i++) {
            cin>>s1;
            //cout<<s<<endl;
            addr.insert(s1+'@'+s);
            //cout<<s1+'@'+s<<endl;
        }
    }

    //Input sender
    string sendName, sendCity;
    string reciName, reciCity;
    while(cin>>s && s!="*") {
        cut(s, sendName, sendCity);
        //cout<<"sendName="<<vname[sendName]<<",sendCity="<<vcity[sendCity]<<endl;

        //Enter recipient
        dest.clear();   //Receiving city vector < name >
        city.clear();   //Send to receiving city in order
        vis.clear();    //Determine whether the receiving address is duplicate
        while(cin>>s && s!="*") {
            //Prevent recipients from repeating
            if(vis.count(s)) continue;
            vis.insert(s);

            cut(s, reciName, reciCity);
            //cout<<"reciName="<<vname[reciName]<<",reciCity="<<vcity[reciCity]<<endl;
            if(!dest.count(reciCity)) {
                dest[reciCity] = vector<string>();
                city.push_back(reciCity);
            }
            dest[reciCity].push_back(reciName);
        }

        //Input information
        string data;
        getline(cin, s);
        while(getline(cin, s) && s!="*") {
            data += "     " + s + "\n";
        }
        //cout<<data<<endl;

        //Output message
        //Output one city per for cycle
        for(int i=0; i<city.size(); i++) {
            cout<<"Connection between "<<sendCity<<" and "<<city[i]<<endl;
            //Output command
            cout<<"     HELO " << sendCity <<endl;
            cout<<"     250"   <<endl;

            cout<<"     MAIL FROM:<" << (sendName+'@'+sendCity) <<'>'<<endl;
            cout<<"     250"         <<endl;

            int canreci = 0;
            for(int j=0; j<dest[city[i]].size(); j++) {
                cout<<"     RCPT TO:<" << (dest[city[i]][j]+'@'+city[i]) <<'>'<<endl;
                if(addr.count(dest[city[i]][j]+'@'+city[i])) {
                    cout<<"     250" <<endl;
                    canreci++;
                }
                else cout<<"     550" <<endl;
            }

            if(canreci) {
                cout<<"     DATA" <<endl;
                cout<<"     354"  <<endl;
                cout<<data<<"     ."<<endl;
                cout<<"     250" <<endl;
            }

            cout<<"     QUIT" <<endl;
            cout<<"     221"  <<endl;
        }
    }
    return 0;
}

Posted on Mon, 02 Dec 2019 14:37:30 -0800 by mingwww