【刘汝佳书】例题5-11 UVA814(字符串分割,判重等)

【2019.4.5】
注意:可能会有重复的收件地址
第一次没考虑到这点,没过,后来看了书上的代码才发现还要判重

一开始打算把字符串编号,感觉自己已经魔怔了
深刻感觉到数据结构的重要性,选择好的组织数据的方法真的很重要
这题抄书真的很有挫败感,凌晨AC真的郁闷

分割字符串的写法:

void cut(const string& s, string& name, string& city)
{
    int k = s.find('@');    //s[k]=='@'
    name = s.substr(0, k);  //0到k,不包括k
    city = s.substr(k+1);   //k+1到结尾
}

全部代码:

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

using namespace std;

set<string> addr;   //所有合法的收信地址,name@city

map<string, vector<string>> dest;   //接收城市city-vector<name>
vector<string> city;                //按照顺序,发给接收城市

set<string> vis;        //判断收信地址是否重复

void cut(const string& s, string& name, string& city)
{
    int k = s.find('@');    //s[k]=='@'
    name = s.substr(0, k);  //0到k,不包括k
    city = s.substr(k+1);   //k+1到结尾
}

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;

    //输入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;
        }
    }

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

        //输入接收者
        dest.clear();   //接收城市city-vector<name>
        city.clear();   //按照顺序,发给接收城市
        vis.clear();    //判断收信地址是否重复
        while(cin>>s && s!="*") {
            //防止收信人重复
            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);
        }

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

        //输出消息
        //每个for循环输出一个城市
        for(int i=0; i<city.size(); i++) {
            cout<<"Connection between "<<sendCity<<" and "<<city[i]<<endl;
            //输出命令
            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;
}

猜你喜欢

转载自blog.csdn.net/qq_41727666/article/details/89057691