题目
#define _CRT_SECURE_NO_WARNINGS
#define LOCAL 1
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void parse_address(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
freopen("C:/Users/pyliu/代码/Visual stdio/语言篇/STL入门/竞赛题目/邮件传输代理的交互 The Letter Carrier's Rounds/input.txt", "r", stdin);
freopen("C:/Users/pyliu/代码/Visual stdio/语言篇/STL入门/竞赛题目/邮件传输代理的交互 The Letter Carrier's Rounds/output.txt", "w", stdout);
#endif // LOCAL
int k;
string s, t, user1, user2, mta1, mta2;
set<string> addr;//存放完整的用户邮箱
while (cin >> s && s != "*") { //读取地址 及 包含的用户 此处s读取为MTA
cin >> s >> k; //地址s有k个用户
while (k--) { cin >> t; addr.insert(t + "@" + s); }
}
while (cin >> s && s != "*") { //多次处理发件人
parse_address(s, user1, mta1); //发件人的username 和mta
/* 需要收件人的完整邮箱判断是否有重复收件人 set<string>
* 需要mta输出顺序 vector<string>
* 需要每个mta对应的收件人邮箱 map<string,vector<string>>
* 根据mta输出顺序找到其对应的收件人的全部邮箱,再判断是否输出
*/
vector<string> mta;
set<string> vis;
map<string, vector<string>> dest;
while (cin >> t && t != "*") { //处理多个收件人
parse_address(t, user2, mta2); //收件人的username 和mta
if (vis.count(t)) continue;
vis.insert(t);
if (!dest.count(mta2)) {
dest[mta2] = vector<string>();
mta.push_back(mta2);
}
dest[mta2].push_back(t);
}
//由于正文要读取一整行,用getline函数,因此需要将*和回车吃掉
getline(cin, t);
string data;
while (getline(cin, t) && t != "*") { //正文读取
data += " " + t + "\n";
}
for (int i = 0; i < mta.size(); i++) { //数据输出
mta2 = mta[i]; //收件地址
vector<string> users = dest[mta2];//收件姓名
cout << "Connection between " << mta1 << " and " << mta2 << endl;
cout << " HELO " << mta1 << endl << " 250" << endl;
cout << " MAIL FROM:<" << s << ">\n" << " 250" << endl;
bool flag = false;//判断最后是否有正确的收件人
for (int j = 0; j < users.size(); j++) {
cout << " RCPT TO:<" << users[j] << ">" << endl;
if (addr.count(users[j])) { cout << " 250" << endl; flag = true; }
else cout << " 550" << endl;
}
if (flag) {
cout << " DATA\n" << " 354\n";
cout << data << " .\n 250\n";
}
cout<<" QUIT\n 221\n";
}
}
return 0;
}