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;
}