UVA 814 The Letter Carrier's Rounds

题目链接:https://vjudge.net/problem/UVA-814

题目翻译摘自《算法禁赛入门经典》

题目大意

  本题的任务为模拟发送邮件时 MTA(邮件传输代理)之间的交互。所谓 MTA,就是 email地址格式 [email protected] 的“后面部分”。当某人从 [email protected] 发送给另一个人 [email protected] 时,这两个 MTA 将会通信。如果两个收件人属于同一个 MTA,发送者的 MTA 只需与这个 MTA 通信一次就可以把邮件发送给这两个人。

  输入每个 MTA 里的用户列表,对于每个发送请求(输入发送者和接收者列表),按顺序输出所有 MTA 之间的 SMTP(简单邮件协议)交互。协议细节参见原题。

  发送人 MTA 连接收件人 MTA 的顺序应该与在输入中第一次出现的顺序一致

  如果连接某个 MTA 之后发现所有收件人都不存在,则不应该发送DATA。所有用户名均 由不超过15个字母和数字组成。

  发信人一定存在

分析

  略。

代码如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15
 16 #define LOWBIT(x) ((x)&(-x))
 17
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20
 21 #define ms0(a) memset(a,0,sizeof(a))
 22 #define msI(a) memset(a,inf,sizeof(a))
 23 #define msM(a) memset(a,-1,sizeof(a))
 24
 25 #define MP make_pair
 26 #define PB push_back
 27 #define ft first
 28 #define sd second
 29
 30 template<typename T1, typename T2>
 31 istream &operator>>(istream &in, pair<T1, T2> &p) {
 32     in >> p.first >> p.second;
 33     return in;
 34 }
 35
 36 template<typename T>
 37 istream &operator>>(istream &in, vector<T> &v) {
 38     for (auto &x: v)
 39         in >> x;
 40     return in;
 41 }
 42
 43 template<typename T1, typename T2>
 44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 45     out << "[" << p.first << ", " << p.second << "]" << "\n";
 46     return out;
 47 }
 48
 49 inline int gc(){
 50     static const int BUF = 1e7;
 51     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 52
 53     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 54     return *bg++;
 55 }
 56
 57 inline int ri(){
 58     int x = 0, f = 1, c = gc();
 59     for(; c<48||c>57; f = c==‘-‘?-1:f, c=gc());
 60     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 61     return x*f;
 62 }
 63
 64 template<class T>
 65 inline string toString(T x) {
 66     ostringstream sout;
 67     sout << x;
 68     return sout.str();
 69 }
 70
 71 inline int toInt(string s) {
 72     int v;
 73     istringstream sin(s);
 74     sin >> v;
 75     return v;
 76 }
 77
 78 typedef long long LL;
 79 typedef unsigned long long uLL;
 80 typedef pair< double, double > PDD;
 81 typedef pair< int, int > PII;
 82 typedef pair< int, PII > PIPII;
 83 typedef pair< string, int > PSI;
 84 typedef pair< int, PSI > PIPSI;
 85 typedef set< int > SI;
 86 typedef set< PII > SPII;
 87 typedef vector< int > VI;
 88 typedef vector< VI > VVI;
 89 typedef vector< SI > VSI;
 90 typedef vector< PII > VPII;
 91 typedef map< int, int > MII;
 92 typedef map< int, string > MIS;
 93 typedef map< int, PII > MIPII;
 94 typedef map< PII, int > MPIII;
 95 typedef map< string, int > MSI;
 96 typedef map< string, string > MSS;
 97 typedef map< PII, string > MPIIS;
 98 typedef map< PII, PII > MPIIPII;
 99 typedef multimap< int, int > MMII;
100 typedef multimap< string, int > MMSI;
101 //typedef unordered_map< int, int > uMII;
102 typedef pair< LL, LL > PLL;
103 typedef vector< LL > VL;
104 typedef vector< VL > VVL;
105 typedef priority_queue< int > PQIMax;
106 typedef priority_queue< int, VI, greater< int > > PQIMin;
107 const double EPS = 1e-8;
108 const LL inf = 0x7fffffff;
109 const LL infLL = 0x7fffffffffffffffLL;
110 const LL mod = 1e9 + 7;
111 const int maxN = 1e4 + 7;
112 const LL ONE = 1;
113 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
114 const LL oddBits = 0x5555555555555555;
115
116 struct User{
117     string mail, name, addr;
118
119     User() {}
120     User(string s) {
121         mail = s;
122         name = s.substr(0, s.find(‘@‘));
123         addr = s.substr(s.find(‘@‘) + 1);
124     }
125 };
126
127 MSS mta; // name->address
128
129 int main(){
130     //freopen("MyOutput.txt","w",stdout);
131     //freopen("input.txt","r",stdin);
132     //INIT();
133     string tmp, msg;
134     while(cin >> tmp) {
135         if(tmp == "*") break;
136         string loc;
137         int n;
138
139         cin >> loc >> n;
140         Rep(i, n) {
141             cin >> tmp;
142             mta[tmp] = loc;
143         }
144     }
145
146     while(cin >> tmp) {
147         if(tmp == "*") break;
148         User from(tmp);
149         vector< User > to;
150         map< string, VI > msv; // 顺序存相同地址的人
151         set< string > names; // 判重用
152
153         while(cin >> tmp) {
154             if(tmp == "*") break;
155             User t(tmp);
156
157             if(names.find(t.name) != names.end()) continue;
158             else names.insert(t.name);
159             to.PB(t);
160             msv[t.addr].PB(to.size() - 1);
161         }
162
163         getline(cin, msg); // ‘\n‘
164         while(getline(cin, tmp)) {
165             if(tmp == "*") break;
166             msg += "     " + tmp + "\n";
167         }
168
169         foreach(i, to) {
170             if(msv.find(i->addr) != msv.end()) {
171                 printf("Connection between %s and %s\n", from.addr.c_str(), i->addr.c_str());
172                 printf("     HELO %s\n", from.addr.c_str());
173                 printf("     250\n");
174                 printf("     MAIL FROM:<%s>\n", from.mail.c_str());
175                 printf("     250\n");
176
177                 int cnt = 0;
178                 foreach(j, msv[i->addr]) {
179                     printf("     RCPT TO:<%s>\n", to[*j].mail.c_str());
180                     if(mta.find(to[*j].name) != mta.end()) printf("     250\n"), ++cnt;
181                     else printf("     550\n");
182                 }
183                 msv.erase(i->addr);
184
185                 if(cnt) {
186                     printf("     DATA\n");
187                     printf("     354\n");
188                     printf("%s", msg.c_str());
189                     printf("     .\n");
190                     printf("     250\n");
191                 }
192
193                 printf("     QUIT\n");
194                 printf("     221\n");
195             }
196         }
197     }
198     return 0;
199 }

UVA 814 The Letter Carrier's Rounds

原文地址:https://www.cnblogs.com/zaq19970105/p/11063869.html

时间: 2024-10-30 16:00:35

UVA 814 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

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

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

The Letter Carrier&#39;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 com

紫書-第五章-例題

衹爲總結,不爲其他,敲過代碼,但也可能會遺忘,權當加深印象了: 5-1 大理石在哪裏(UVa 10474) 很容易的題目,sort+lower_bound,不過自己對lower_bound還是用的很少,欸,所以還是不太敢用的= =: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int m

java常见算法题目

1: JAVA经典算法40题 2: [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 3: 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... 4: public class exp2{ 5: public static void main(String args[]){ 6: int i=0; 7: for(i=1;i<=20;i++) 8: System.o

UVa 11577 - Letter Frequency

题目:给你一句话,统计里面出现最多的字母,有多个按字典序输出. 分析:简单题.直接统计即可. 说明:注意输入格式. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int main() { int n,count[2

UVa Online Judge 12532 - Interval Product

UVA - 12532 Interval Product http://acm.bnu.edu.cn/v3/problem_show.php?pid=27853 Interval Product Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 12532 64-bit integer IO format: %lld Java class name: Main Pr

UVA - 12532 Interval Product

http://acm.bnu.edu.cn/v3/problem_show.php?pid=27853 Interval Product Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1253264-bit integer IO format: %lld Java class name: Main Prev Submit Status Statistics Di

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED