算典05_例题_11_UVA-814

The Letter Carrier’s Rounds

题意

本题的任务为模拟发送邮件时MTA(邮件传输代理)之间的交互。所谓MTA,就是email地址格式user@mtaname的“后面部分”。当某人从user1@mta1发送给另一个人user2@mta2时,这两个MTA将会通信。如果两个收件人属于同一个MTA,发送者的MTA只需与这个MTA通信一次就可以把邮件发送给这两个人。

题解

map
set
看代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a, b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5;
const int INF = (1 << 31) - 1;

string s, t, user1, mta1, user2, mta2;
int k;
set<string> addr;

void solve(const string& s, string& user, string& mta) {
    int k = s.find('@');
    user = s.substr(0,k);
    mta = s.substr(k+1);
}

int main() {
    #ifdef _LOCAL
    IN;
    #endif // _LOCAL

    //insert
    while(cin >> s && s != "*") {
        cin >> s >> k;
        while(k--) {cin >> t; addr.insert(t + "@" + s); }
    }

    while(cin >> s && s != "*") {
        solve(s, user1, mta1);

        vector<string> mta;
        map<string, vector<string> > dest;
        set<string> vis;

        while(cin >> t && t != "*") {
            solve(t, user2, mta2);
            if(vis.count(t)) continue;
            vis.insert(t);
            if(!dest.count(mta2)) {
                mta.push_back(mta2);
                dest[mta2] = vector<string>();
            }
            dest[mta2].push_back(t);
        }
        getline(cin, t);

        string data;
        while(getline(cin, t) && t[0] != '*') data += "     " + t + "\n";

        for(int i = 0; i < mta.size(); i++) {
            string mta2 = mta[i];
            vector<string> users = dest[mta2];
            cout << "Connection between " << mta1 << " and " << mta2 <<endl;
            cout << "     HELO " << mta1 << "\n";
            cout << "     250\n";
            cout << "     MAIL FROM:<" << s << ">\n";
            cout << "     250\n";
            bool ok = false;
            for(int i = 0; i < users.size(); i++) {
                cout << "     RCPT TO:<" << users[i] << ">\n";
                if(addr.count(users[i])) {
                    ok = true;
                    cout << "     250\n";
                } else cout << "     550\n";
            }
            if(ok) {
                cout << "     DATA\n";
                cout << "     354\n";
                cout << data;
                cout << "     .\n";
                cout << "     250\n";
            }
            cout << "     QUIT\n";
            cout << "     221\n";
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值