The Letter Carrier's Rounds UVA - 814 个人见解

2024-01-02 05:08

本文主要是介绍The Letter Carrier's Rounds UVA - 814 个人见解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先上题目。
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


这道题目是刘汝佳大神的《算法竞赛入门经典》书中(因为这本书封面是紫色的,所以我又将这本称作紫书)的一道让我们熟悉C++STL用法的一道题目。紫书中已经给出了示例代码和题解。我的AC代码也是顺着刘神的思路写的。其中用到了**C++11新标准的新特性**,编译环境是开了C++11标准的CodeBlocks&mingw32-g++。如果你的编译器不通过的话,开一下编译器的C++11标准,在CodeBlocks16.01编译器菜单栏,点击Settings->Compiler,打开Global compiler settings界面,将"Have g++ follow the C++11 ISO C++ language"选项打上勾,最后点击OK。或者去掉代码中的C++11新特性语法就好了。
题目的意思大概就是模拟发送邮件时MTA之间的交互过程。英文好的看原题目就懂了,不懂的就有道翻译一下。理解透了题目意思,这道题就迎刃而解了。
下面给出我的AC代码:
#include<iostream>
#include<set>
#include<string>
#include<vector>
#include<map>using namespace std;const string Space = "     ";     //五个空格int main()
{int n{ 0 };string str1, str2;set<string>address, vis;    //address->存放所有的MTA经处理后要联系的地址,vis->存放从address中已经解析过的地址vector<string>mta,users;    //mta->所有需要连接的MTA  users->存放从post中取出的某个mta对应的要联系的所有用户map<string, vector<string> >post;//存放每个MTA需要发送的用户,注意"vector<string> >post"中后面的两个">"中间要间隔一个空格,以免有些编译器误认为是“>>”操作符while (cin >> str1 && str1 != "*") {//读入数据cin >> str1 >> n;while (n--) {cin >> str2;address.insert(str2 + "@" + str1);//处理地址,注意括号里是str2在前面,否则联系人搞反了可就wrong answer了}}while (cin >> str1 && str1 != "*") {mta.clear();       //清除各个容器vis.clear();post.clear();auto pos = str1.find('@');auto mta1 = str1.substr(pos + 1); //解析MTAwhile (cin >> str2 && str2 != "*") {pos = str2.find('@');auto mta2 = str2.substr(pos + 1);if (vis.count(str2))continue; //如果前面已经解析过这个地址了就直接跳过这个地址vis.insert(str2);  //每解析一个地址,就把这个地址记录下来,防止重复解析if (!post.count(mta2)) {//如果是一个新的MTA就存入mta中mta.push_back(mta2);post[mta2] = vector<string>();}post[mta2].push_back(str2);}string email, line;getline(cin, line);//“吃掉”第一封邮件正文内容前一行while (getline(cin, line) && line[0] != '*') {email += Space + line + "\n";  //将邮件内容按输出格式处理后存到string变量email中}for (vector<string>::size_type i = 0; i != mta.size(); i++) {string mta3 = mta[i];users.clear();//这个清除一定不要忘了users = post[mta3];cout << "Connection between " << mta1 << " and " << mta3 << endl;cout << Space << "HELO " << mta1 << endl;cout << Space << "250" << endl;cout << Space << "MAIL FROM:<" << str1 << ">" << endl;cout << Space << "250" << endl;bool ok{ false };for (vector<string>::size_type j = 0; j != users.size(); j++) {cout << Space<<"RCPT TO:<" << users[j] << ">" << endl;if (address.count(users[j])) {//判断是否找到了对应的联系人地址ok = true;cout << Space << "250" << endl;}else cout << Space << "550" << endl;}if (ok) {//找到了就输出邮件内容cout << Space << "DATA" << endl;cout << Space << "354" << endl;cout << email << Space <<"." << endl;cout << Space << "250" << endl;}cout << Space << "QUIT" << endl;cout << Space << "221" << endl;}}return 0;
}

这篇关于The Letter Carrier's Rounds UVA - 814 个人见解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/561420

相关文章

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=