MEGChai MEGChai
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
首页 › 数据结构与算法 › 在线评测 › UVaOJ 814 - The Letter Carrier's Rounds
AOAPC II

UVaOJ 814 - The Letter Carrier's Rounds

Chai
2021-11-24 0:00:00在线评测阅读 453

问题描述

p814

原题链接:UVaOJ 814 - The Letter Carrier's Rounds

相关说明:本题为《算法竞赛入门经典(第2版)》例题 5-11

解法一:模拟

题目首先给出了一些 MTA 信息,表明这些地址是可以收到邮件的。接着给出一行邮箱地址,第一个邮箱地址是发送方,其余为拟定的接收方(不一定真实存在),紧跟着需要发送的信息。邮箱代理根据接收方邮箱的地址输入顺序(而不是字典序)进行发送信息,且保证同样的邮箱只会发送一次(这里直接用 set 去重即可),如果有邮箱地址是存在于 MTA 中的,则表示信息可以发送出去,输出 DATA 内容。最后退出。

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
  map<string, unordered_set<string>> mta;
  string tmp, addr, name;
  int num;
 
  while (cin >> tmp) {
    // Build the MTA informations
    do {
      cin >> addr >> num;
      for (int i = 0; i < num; ++i) cin >> name, mta[addr].insert(name);
    } while (cin >> tmp && tmp != "*");
    getchar();
 
    while (getline(cin, tmp) && tmp != "*") {
      // Get mail informations
      map<string, vector<string>> a2name;  // address to name
      vector<string> addrs;
      string send_addr, send_name;
      stringstream line(tmp);
      bool is_sending = true;
      while (line >> tmp) {
        int idx = tmp.find('@');
        name = tmp.substr(0, idx);
        addr = tmp.substr(idx + 1);
        if (is_sending) {
          send_name = name;
          send_addr = addr;
          is_sending = false;
        } else {
          if (a2name.find(addr) == a2name.end()) addrs.push_back(addr);
          a2name[addr].push_back(name);
        }
      }
 
      // Check receiver status and send
      getline(cin, tmp);
      string message, lead_blank = "     ";
      while (getline(cin, tmp) && tmp != "*")
        message += (lead_blank + tmp + "\n");
      for (auto a : addrs) {
        cout << "Connection between " << send_addr << " and " << a << endl;
        cout << lead_blank << "HELO " << send_addr << endl;
        cout << lead_blank << "250" << endl;
        cout << lead_blank << "MAIL FROM:<" << send_name << "@" << send_addr
             << ">" << endl;
        cout << lead_blank << "250" << endl;
 
        int num_rec = 0;
        set<string> _set;
        for (auto n : a2name[a]) {
          if (_set.find(n) == _set.end())
            _set.insert(n);
          else
            continue;
 
          cout << lead_blank << "RCPT TO:<" << n << "@" << a << ">" << endl;
          if (mta[a].find(n) != mta[a].end()) {
            cout << lead_blank << "250" << endl;
            num_rec += 1;
          } else {
            cout << lead_blank << "550" << endl;
          }
        }
 
        if (num_rec) {
          cout << lead_blank << "DATA" << endl;
          cout << lead_blank << "354" << endl;
          cout << message << lead_blank << "." << endl;
          cout << lead_blank << "250" << endl;
        }
 
        cout << lead_blank << "QUIT" << endl;
        cout << lead_blank << "221" << endl;
      }
    }
  }
  return 0;
}
Python
1
#TODO
AOAPC II UVaOJ
赞赏 赞(0)
订阅
提醒
guest
guest
0 评论
内嵌评论
查看所有评论
  • 0
  • 0
Copyright © 2020-2023 MEGChai.
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
# 生活 # # 心理 # # 编程 # # 音乐 # # 写作 #
Chai
95
文章
4
评论
58
喜欢
wpDiscuz