模拟邮件的发送,把每一个地址保存下来, 然后在搜索能否找到匹配的地址。
#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <map>
#include <vector>
using namespace std;
void parse_address(string const s, string& user, string& mta) {
int t = s.find('@');
user = s.substr(0, t);
mta = s.substr(t + 1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test", "r", stdin);
#endif // ONLINE_JUDGE
int k;
string s, t, user1, user2, mta1, mta2;
set<string> addr;
while(cin >> s&& s[0] != '*') {
cin >> t >> k;
while(k--) {cin >> s; addr.insert(s + '@' + t);}
}
while(cin >> s&& s[0] != '*') {
parse_address(s, user1, mta1);
vector<string> mta;
map<string, vector<string> > dest;
set<string> vis;
while(cin >> t&& t[0] != '*') {
if(vis.count(t)) continue;
vis.insert(t);
parse_address(t, user2, mta2);
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++) {
mta2 = mta[i];
vector<string> users = dest[mta2];
cout << "Connection between " << mta1 << " and " << mta2 << "\n";
cout << " HELO "<< mta1 << "\n";
cout << " 250\n";
cout << " MAIL FROM:<" << user1 << "@" << mta1 << ">\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 354\n";
cout << data;
cout << " .\n" << " 250\n";
}
cout << " QUIT\n 221\n";
}
}
return 0;
}