The Letter Carrier's Rounds(摘)

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 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 [email protected] 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 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
*
[email protected] [email protected] [email protected] [email protected]
*
Congratulations on your efforts !!
--Hamdy
*
[email protected] [email protected] [email protected]
*
Thanks for the report!  --Fiona
*
*

Sample Output

Connection between Cairo and MexicoCity
     HELO Cairo
     250
     MAIL FROM:<[email protected]>
     250
     RCPT TO:<[email protected]>
     250
     RCPT TO:<[email protected]>
     550
     DATA
     354
     Congratulations on your efforts !!
     --Hamdy
     .
     250
     QUIT
     221
Connection between Cairo and SanFrancisco
     HELO Cairo
     250
     MAIL FROM:<[email protected]>
     250
     RCPT TO:<[email protected]>
     250
     DATA
     354
     Congratulations on your efforts !!
     --Hamdy
     .
     250
     QUIT
     221 
Connection between London and HongKong
     HELO London
     250
     MAIL FROM:<[email protected]>
     250
     RCPT TO:<[email protected]>
     250
     DATA
     354
     Thanks for the report!  --Fiona
     .
     250
     QUIT
     221
Connection between London and Paris
     HELO London
     250
     MAIL FROM:<[email protected]>
     250
     RCPT TO:<[email protected]>
     550
     QUIT
     221

就是数据太复杂,对于stl中容器的应用很生疏,没啥思维难点

以下程序的亮点:

用一个string字符串data存储两三行的邮件

#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<set>
using namespace std;

void parse_address(const string&s,string& user,string&mta)
{
    int k=s.find(‘@‘);
    user=s.substr(0,k);            //将[email protected] 分解开来
    mta=s.substr(k+1);
}
int main()
{
    int n;
    string s,t,user1,mta1,user2,mta2;
    set<string>address;

    while(cin>>s&&s!="*"){                //mta下的用户信息
        cin>>s>>n;
        while(n--){
            cin>>t;
            address.insert(t+‘@‘+s);
        }
    }
    while(cin>>s&&s!="*"){
        parse_address(s,user1,mta1);         //发件人

        vector<string>mta;                   //所有需要连接的mta,按照输入顺序
        map<string,vector<string>>dest;      //每个mta需要发送的用户
        set<string>vis;

        while(cin>>t&&t!="*"){                  //收件人
            parse_address(t,user2,mta2);
            if(vis.count(t))continue;          //收件人重复则不插入
            vis.insert(t);
            if(!dest.count(mta2)){             //mta向量中没有该mta就插入并将向量与map中的string mta对应起来
                mta.push_back(mta2);
                dest[mta2]=vector<string>();
            }
            dest[mta2].push_back(t);        //插入[email protected] 完整形式
        }
        getline(cin,t);               //吃掉"*"

        string data;
        while(getline(cin,t)&&t[0]!=‘*‘)data+="  "+t+"\n";      //将整个邮件存入一个字符串data中

        //cout<<"***"<<data<<"***"<<endl;
        for(int i=0;i<mta.size();i++){
            string mta2=mta[i];
            vector<string>users=dest[mta2];                 //users即为dest[mata2],mata2对应的需要发送的用户
            cout<<"Connection between "<<mta1<<" and "<<mta2<<endl;
            cout<<"  HELO "<<mta1<<"\n";
            cout<<"  250\n";
            cout<<"  MALL FROM:<" <<s<<">\n";
            cout<<"  250\n";
            bool ok=false;
            for(int i=0;i<users.size();i++){
                cout<<"RCPT TO:<"<<users[i]<<">\n";
                if(address.count(users[i])){
                    ok=true;
                    cout<<"  250\n";
                }
                else cout<<"  500\n";
            }
            if(ok){
                cout<<"  DATA\n";
                cout<<"  254\n";
                cout<<data;
                cout<<".\n";
                cout<<"  250\n";
            }
            cout<<"  QUIT\n";
            cout<<"  221\n";
        }
    }
    //system("pause");
    return 0;
}

The Letter Carrier's Rounds(摘)

时间: 2024-10-11 05:13:53

The Letter Carrier's Rounds(摘)的相关文章

UVA 814 The Letter Carrier&#39;s Rounds(JAVA基础map)

题解:就是按照题目模拟就好 但是这个题目让我发现了我Java里面许多问题 具体看代码,但是还是分为这几个方面 属性的作用域问题,缓冲区问题,map与list映射的问题,输出多个空格不一定是/t,反转思想代码优化 import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Sc

UVA 814 The Letter Carrier&#39;s Rounds

题目链接:https://vjudge.net/problem/UVA-814 题目翻译摘自<算法禁赛入门经典> 题目大意 本题的任务为模拟发送邮件时 MTA(邮件传输代理)之间的交互.所谓 MTA,就是 email地址格式 [email protected] 的“后面部分”.当某人从 [email protected] 发送给另一个人 [email protected] 时,这两个 MTA 将会通信.如果两个收件人属于同一个 MTA,发送者的 MTA 只需与这个 MTA 通信一次就可以把邮件

The Letter Carrier&#39;s Rounds UVA - 814

记这题主要是想记录两条经验,一个是要考虑数据的可重性,删去重复数据:二是跟上篇博客一样的错误,数组复写导致数据交叉而引起的奇妙bug.以后在类似复写情况要先考虑结尾元素,这两次都栽到这里,因为结尾元素没有更新但却用了...一定要记得把要用的数据但未更新的初始化,主要是考察当前所要使用数据的范围有无超出更新的范围. #include <iostream> #include <cstdio> #include <cstring> #include <map> #

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

陶陶摘苹果(升级版)

题目描述 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他手够不着时,他会站到椅子上再试试. 这次与NOIp2005普及组第一题不同的是:陶陶之前搬凳子,力气只剩下s了.当然,每次摘苹果时都要用一定的力气.陶陶想知道在s<0之前最多能摘到多少个苹果. 现在已知n个苹果到达地上的高度xi,椅子的高度a,陶陶手伸直的最大长度b,陶陶所剩的力气s,陶陶摘一个苹果需要的力气yi,求陶陶最多能摘到多少个苹果. 输入输出格式 输入格式: 第1行:两个数 苹果数n,

[USACO 2011 Dec Gold] Threatening Letter【后缀】

Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and wants to send him a nasty letter, but wants to remain anonymous. As so many before him have done, he plans to cut out printed letters and paste them on

树形数组——摘星星伪题解

树形数组. 题目:摘星星 描述:宇航员经常检测星图,在星图上,星星由点表示而且每颗星星都有笛卡尔坐标.星星的等级表示左下方星星的数量.宇航员想知道星星等级的分布. 例如,如上面图形所示,第5号星等级是3 (它由三个标记为1,2和4的星组成).标记着2和4的星星的等级是1,在此地图上,0等级的星星只有一个,1等级的有两个,2等级的有一个,3等级的有一个. 你设计一个程序,在给定地图上计算出每个等级星星的数量. 输入: 输入文件“INPUT.TXT”的第一行包含N个星星(1<=N<=60000),

Letter Combinations of a Phone Number

1. Question 给一个数字字符串,按照电话上各个数字对应的字母,返回该字符串代表的所有可能的字母组合. Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit stri

【leetcode】Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outpu