For an electronic mail application you are to describe the SMTP-based communication that takes place between pairs of MTAs. The sender’s User Agent gives a formatted message to the sending Message Transfer Agent (MTA). The sending MTA communicates with the receiving MTA using the Simple Mail Transfer Protocol (SMTP). The receiving MTA delivers mail to the receiver’s User Agent. After a communication link is initialized, the sending MTA transmits command lines, one at a time, to the receiving MTA, which returns a three-digit coded response after each command is processed. The sender commands are shown below in the order sent for each message. There is more than one RCPT TO line when the same message is sent to several users at the same MTA. A message to users at different MTAs requires separate SMTP sessions.
HELO myname Identifies the sender to the receiver (yes, there is only one L)
MAIL FROM:< sender > Identifies the message sender
RCPT TO:< user > Identifies one recipient of the message
DATA Followed by an arbitrary number of lines of text comprising the message body, ending with a line containing a period in column one.
QUIT Terminates the communication.
The following response codes are sent by the receiving MTA:
221 Closing connection (after QUIT)
250 Action was okay (after MAIL FROM and RCPT TO specifying an acceptable user, or completion of a message)
354 Start sending mail (after DATA)
550 Action not taken; no such user here (after RCPT TO with unknown user)
Input
The input contains descriptions of MTAs followed by an arbitrary number of messages. Each MTA description begins with the MTA designation and its name (1 to 15 alphanumeric characters). Following the MTA name is the number of users that receive mail at that MTA and a list of the users (1 to 15 alphanumeric characters each). The MTA description is terminated by an asterisk in column 1. Each message begins with the sending user’s name and is followed by a list of recipient identifiers. Each identifier has the form user@mtaname. The message (each line containing no more than 72 characters) begins and terminates with an asterisk in column 1. A line with an asterisk in column 1 instead of a sender and recipient list indicates the end of the entire input.
Output
For each message, show the communication between the sending and receiving MTAs. Every MTA mentioned in a message is a valid MTA; however, message recipients may not exist at the destination MTA. The receiving MTA rejects mail for those users by responding to the RCPT TO command with the 550 code. A rejection will not affect delivery to authorized users at the same MTA. If there is not at least one authorized recipient at a particular MTA, the DATA is not sent. Only one SMTP session is used to send a message to users at a particular MTA. For example, a message to 5 users at the same MTA will have only one SMTP session. Also a message is addressed to the same user only once. The order in which receiving MTAs are contacted by the sender is the same as in the input file. As shown in the sample output, prefix the communication with the communicating MTA names, and indent each non-empty communication line. No innecessary spaces should be printed.
Sample Input
MTA London 4 Fiona Paul Heather Nevil
MTA SanFrancisco 3 Mario Luigi Shariff
MTA Paris 3 Jacque Suzanne Maurice
MTA HongKong 3 Chen Jeng Hee
MTA MexicoCity 4 Conrado Estella Eva Raul
MTA Cairo 3 Hamdy Tarik Misa
*
Hamdy@Cairo Conrado@MexicoCity Shariff@SanFrancisco Lisa@MexicoCity
*
Congratulations on your efforts !!
--Hamdy
*
Fiona@London Chen@HongKong Natasha@Paris
*
Thanks for the report! --Fiona
*
*
Sample Output
Connection between Cairo and MexicoCity
HELO Cairo
250
MAIL FROM:<Hamdy@Cairo>
250
RCPT TO:<Conrado@MexicoCity>
250
RCPT TO:<Lisa@MexicoCity>
550
DATA
354
Congratulations on your efforts !!
--Hamdy
.
250
QUIT
221
Connection between Cairo and SanFrancisco
HELO Cairo
250
MAIL FROM:<Hamdy@Cairo>
250
RCPT TO:<Shariff@SanFrancisco>
250
DATA
354
Congratulations on your efforts !!
--Hamdy
.
250
QUIT
221
Connection between London and HongKong
HELO London
250
MAIL FROM:<Fiona@London>
250
RCPT TO:<Chen@HongKong>
250
DATA
354
Thanks for the report! --Fiona
.
250
QUIT
221
Connection between London and Paris
HELO London
250
MAIL FROM:<Fiona@London>
250
RCPT TO:<Natasha@Paris>
550
QUIT
221
题目类型:简单字符处理+模拟
算法分析:按照题目给定的过程模拟MTA交互即可,注意若接受者中有重复的地址是需要去除掉的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#include <iostream> #include <fstream> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> using namespace std; #define EPS 1e08; void GetStringParse (string &s, string &user, string &mta) { int weizhi = s.find ('@'); user = s.substr (0, weizhi); mta = s.substr (weizhi + 1); } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); set <string> Set; string input_a, input_b, user1, mta1, user2, mta2; while (cin >> input_a) { if (input_a == "*") break; int num; cin >> input_a >> num; while (num--) { cin >> input_b; Set.insert (input_b + "@" + input_a); } } while (cin >> input_a) { if (input_a == "*") break; GetStringParse (input_a, user1, mta1); map <string, vector <string> > dict; vector <string> mta; set <string> vis; while (cin >> input_b) { if (input_b == "*") break; GetStringParse (input_b, user2, mta2); if (vis.count (input_b)) continue; vis.insert (input_b); if (!dict.count (mta2)) { mta.push_back (mta2); dict[mta2] = vector <string> (); } dict[mta2].push_back (input_b); } getline (cin, input_b); string data; while (getline (cin, input_b)) { if (input_b[0] == '*') break; data += " " + input_b + "\n"; } int i; for (i = 0; i < mta.size (); i++) { string val_a = mta[i]; vector <string> val_b = dict[val_a]; cout << "Connection between " << mta1 << " and " << val_a << endl; cout << " HELO " << mta1 << endl << " 250" << endl; cout << " MAIL FROM:<" << input_a << ">" << endl << " 250" << endl; int j; bool is_ok = false; for (j = 0; j < val_b.size (); j++) { cout << " RCPT TO:<" << val_b[j] << ">" << endl; if (Set.count (val_b[j])) { is_ok = true; cout << " 250" << endl; } else cout << " 550" << endl; } if (is_ok) { cout << " DATA" << endl << " 354" << endl << data << " ." << endl << " 250" << endl; } cout << " QUIT" << endl << " 221" << endl; } } return 0; } |