814 - The Letter Carrier's Rounds

For anelectronic mail application you are to describe the SMTP-based communicationthat takes place between pairs of MTAs. The sender's User Agent gives aformatted message to the sending Message Transfer Agent (MTA). The sending MTAcommunicates with the receiving MTA using the Simple Mail Transfer Protocol(SMTP). The receiving MTA delivers mail to the receiver's User Agent. After acommunication link is initialized, the sending MTA transmits command lines, oneat a time, to the receiving MTA, which returns a three-digit coded responseafter each command is processed. The sender commands are shown below in theorder sent for each message. There is more than one RCPT TO line when the samemessage is sent to several users at the same MTA. A message to users atdifferent MTAs requires separate SMTP sessions.

 

HELO            myname Identifies the sender tothe receiver (yes, there is only one L)


MAIL             FROM:< sender > Identifiesthe message sender


RCPT             TO:< user > Identifies onerecipient of the message


DATA                          Followed by an arbitrary number of lines oftext comprising the message body, ending with a line containing a period incolumn one.


QUIT                          Terminates the communication.

The following responsecodes are sent by the receiving MTA:

221

Closing connection (afterQUIT)

250

Action was okay (afterMAIL FROM and RCPT TO specifying an acceptable user, or completion of amessage)

354

Start sending mail (afterDATA)

550

Action not taken; no suchuser here (after RCPT TO with unknown user)

Input 

The input containsdescriptions of MTAs followed by an arbitrary number of messages. Each MTAdescription begins with the MTA designation and its name (1 to 15 alphanumericcharacters). Following the MTA name is the number of users that receive mail atthat MTA and a list of the users (1 to 15 alphanumeric characters each). TheMTA description is terminated by an asterisk in column 1. Each message beginswith the sending user's name and is followed by a list of recipientidentifiers. Each identifier has the form user@mtaname. The message(each line containing no more than 72 characters) begins and terminates with anasterisk in column 1. A line with an asterisk in column 1 instead of a senderand recipient list indicates the end of the entire input.

Output 

For each message, show thecommunication between the sending and receiving MTAs. Every MTA mentioned in amessage is a valid MTA; however, message recipients may not exist at thedestination MTA. The receiving MTA rejects mail for those users by respondingto the RCPT TO command with the 550 code. A rejection will not affect deliveryto authorized users at the same MTA. If there is not at least one authorizedrecipient at a particular MTA, the DATA is not sent. Only one SMTP session isused to send a message to users at a particular MTA. For example, a message to5 users at the same MTA will have only one SMTP session. Also a message isaddressed to the same user only once. The order in which receiving MTAs arecontacted by the sender is the same as in the input file. As shown in thesample output, prefix the communication with the communicating MTA names, andindent each non-empty communication line. No unnecessary spaces should beprinted.

Sample Input 

MTA London 4 Fiona Paul Heather Nevil

MTASanFrancisco 3 Mario Luigi Shariff

MTAParis 3 Jacque Suzanne Maurice

MTAHongKong 3 Chen Jeng Hee

MTAMexicoCity 4 Conrado Estella Eva Raul

MTACairo 3 Hamdy Tarik Misa

*

Hamdy@CairoConrado@MexicoCity Shariff@SanFrancisco Lisa@MexicoCity

*

Congratulationson your efforts !!

--Hamdy

*

Fiona@LondonChen@HongKong Natasha@Paris

*

Thanksfor the report!  --Fiona

*

*

Sample Output 

Connection between Cairoand MexicoCity

     HELO Cairo

     250

     MAILFROM:

     250

     RCPTTO:

     250

     RCPTTO:

     550

     DATA

     354

     Congratulations on your efforts !!

     --Hamdy

     .

     250

     QUIT

     221

Connection between Cairoand SanFrancisco

     HELO Cairo

     250

     MAIL FROM:

     250

     RCPT TO:

     250

     DATA

     354

     Congratulations on your efforts !!

     --Hamdy

     .

     250

     QUIT

     221

Connection between Londonand HongKong

     HELO London

     250

     MAIL FROM:

     250

     RCPT TO:

     250

     DATA

     354

     Thanks for the report!  --Fiona

     .

     250

     QUIT

     221

Connection between Londonand Paris

     HELO London

     250

     MAIL FROM:

     250

     RCPT TO:

     550

     QUIT

     221

代码:

#include

#include

#include

#include

#include

using namespacestd;

 

setaddr;

 

void tran(string &s,string&user,string &mta);

 

int main()

{

    string s1,s2;

    int a;

   

   while(cin>>s1&&s1!="*")

    {

        cin>>s1>>a;

        while(a--)

        {

            string temp;

            cin>>temp;

            temp=temp+"@"+s1;

            addr.insert(temp);

        }

    }

   

   while(cin>>s1&&s1!="*")

    {

        string user1,mta1;

        tran(s1,user1,mta1);

 

        vector mta;

        map > dest;

        set vis;

 

        string user2,mta2;

       while(cin>>s2&&s2!="*")

        {

            if(vis.count(s2))

            {

                continue;

            }

            vis.insert(s2);

            tran(s2,user2,mta2);

            if(dest.count(mta2)==0)

            {

                mta.push_back( mta2);

                dest[mta2]=vector();

            }

            dest[mta2].push_back(s2);

        }

       

        cin.get();

        string data;

        string temp;

       while(getline(cin,temp)&&temp!="*")

        {

            data=data+"     "+temp+"\n";

        }

       

        for(int i=0;i

        {

            string mta22=mta[i];

            cout<<"Connectionbetween "<

            cout<<"     HELO "<

            cout<<"     250\n";

            cout<<"     MAIL FROM:<"<\n";

            cout<<"     250\n";

           

            vector  users=dest[mta22];

            bool flag=false;

            for(int i=0;i

            {

                cout<<"     RCPTTO:<"<\n";

                if(addr.count(users[i]))

                {

                    flag=true;

                    cout<<"     250\n";

                }

                else

                {

                    cout<<"     550\n";

                }

            }

           

            if(flag)

            {

                cout<<"     DATA\n";

                cout<<"     354\n";

                cout<

                cout<<"     .\n";

                cout<<"     250\n";

            }

           

            cout<<"     QUIT\n";

            cout<<"     221\n";

        }

    }

   

    return 0;

}

 

void tran(string&s,string &user,string &mta)

{

    int loc=s.find('@');

    user=s.substr(0,loc);

    mta=s.substr(loc+1,100);

}

解析:

题意:注意如果收件人都不存在,不发送数据.

此题相当繁琐,关键在于理清楚各个名词之间的逻辑关系,然后将解题分为以下几个步骤:

1.       读入所有用户地址,存取在set addr中,存取的形式为:username@MTAname

2.       读入发件人的地址,并利用函数void tran(string &s,string &user,string&mta)分离得到用户名字/MTA名字(输出要用到)

3.       读入收件人的地址:用vector mta按照输入顺序存取所有的MTA名字;用map >dest:键为不同的MTA的名字,值为存取对应MTA 的所有收件人的地址;用set vis:存取收件人完整的地址主要用它来去掉重复的收件人地址

4.       读入邮件正文

5.       输出,注意格式并判断收件人是否存在。

你可能感兴趣的:(814 - The Letter Carrier's Rounds)