The Letter Carrier's Rounds(UVa 814)

来源:互联网 发布:台湾用大陆网络语言 编辑:程序博客网 时间:2024/05/21 10:45
The Letter Carrier's Rounds
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 331 Accepted: 104

Description

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 mynameIdentifies the sender to the receiver (yes, there is only one L).MAIL FROM:<sender>Identifies the message senderRCPT TO:<user>Identifies one recipient of the messageDATAFollowed by an arbitrary number of lines of text comprising the message body, ending with a line containing a period in column one.QUITTerminates the communication.

The following response codes are sent by the receiving MTA: 
221Closing connection (after QUIT)250Action was okay (after MAIL FROM and RCPT TO specifying an acceptable user, or completion of a message)354Start sending mail (after DATA)550Action 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 unspecified. As shown in the sample output , prefix the communication with the communicating MTA names, and indent each communication line.

Sample Input

MTA London 4 Fiona Paul Heather NevilMTA SanFrancisco 3 Mario Luigi Shariff MTA Paris 3 Jacque Suzanne MauriceMTA HongKong 3 Chen Jeng HeeMTA MexicoCity 4 Conrado Estella Eva RaulMTA 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     221Connection 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     221Connection 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     221Connection between London and Paris     HELO London     250     MAIL FROM:<Fiona@London>     250     RCPT TO:<Natasha@Paris>     550     QUIT

221

代码分析:本题考察了对STL和string的灵活运用,细节也很重要,比如用getline(cin, t)一次读取一整行字符,

不要忘记吃掉上一行的回车,用scanf或者getline吃掉上一行留下的回车。自上而下的程序设计思想很重要,对每个函数进行详细的分工,让代码的条理更清晰。详见下代码。

#include<iostream>#include<cstdio>#include<string>#include<set>#include<vector>#include<map>#include <ostream> #include <fstream>using namespace std;set<string> addr;map<string, vector<string> > UserofMTA;//MTA-->每个MTA对应的用户 vector<string> MTA;//所要发送的每个MTA string senduser, receiveuser, sendplace, receiveplace, sendaddr, receiveaddr;void parse_address(string &addr, string &user, string &MTA){int k;k = addr.find('@');user = addr.substr(0, k);MTA = addr.substr(k+1);}void read_user(){string s, MTA, user;int k;while(cin>>s && s!="*"){cin>>MTA>>k;while(k--) {cin>>user;addr.insert(user + "@" + MTA); }}}//返回1结束,返回0继续 int read_cmd(){UserofMTA.clear();MTA.clear();cin >> sendaddr;if(sendaddr=="*") return 1;parse_address(sendaddr, senduser, sendplace);while(cin>>receiveaddr && receiveaddr!="*"){parse_address(receiveaddr, receiveuser, receiveplace);if(!UserofMTA.count(receiveplace)){UserofMTA[receiveplace] = vector<string>();MTA.push_back(receiveplace); }(UserofMTA[receiveplace]).push_back(receiveuser);}return 0;}void read_message(){string message, t;int Do;while(getline(cin, t) && t[0]!='*') //getline(cin, t) 一次读一行 message += "     " + t + "\n";for(int i=0; i<MTA.size(); i++){receiveplace = MTA[i];vector<string> users = UserofMTA[receiveplace];Do = 0;cout << "Connection between "<<sendplace<<" and "<<receiveplace<<endl;cout << "     HELO "<<sendplace<<endl<<"     250"<<endl;cout << "     MAIL FROM:<"<<senduser<<"@"<<sendplace<<">"<<endl<<"     250"<<endl;     for(int j=0; j<users.size(); j++){cout << "     RCPT TO:<"<<(users[j]+"@"+receiveplace)<<">"<<endl; if(addr.count(users[j]+"@"+receiveplace)){cout<< "     250"<<endl;Do=1;}elsecout<< "     550"<<endl;}if(Do){cout<<"     DATA"<<endl<<"     354"<<endl;cout<<message<<"     ."<<endl<<"     250"<<endl;}cout<<"     QUIT"<<endl<<"     221"<<endl;} }int main(){//输入输出重定向 ifstream fin("input5_11.txt");  // 已有输入文件//ofstream fout("output5_11.txt");  //输出文件streambuf *cinbackup;  //streambuf *coutbackup; //coutbackup= cout.rdbuf(fout.rdbuf());  //用 rdbuf() 重新定向cinbackup= cin.rdbuf(fin.rdbuf());  //用 rdbuf() 重新定向read_user();string t;while(read_cmd()==0){getline(cin,t);//把"*"这一行的回车吃掉 read_message();}}


0 0