The Letter Carrier's Rounds UVA - 814
Posted jionkitten
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Letter Carrier's Rounds UVA - 814相关的知识,希望对你有一定的参考价值。
记这题主要是想记录两条经验,一个是要考虑数据的可重性,删去重复数据;二是跟上篇博客一样的错误,数组复写导致数据交叉而引起的奇妙bug。以后在类似复写情况要先考虑结尾元素,这两次都栽到这里,因为结尾元素没有更新但却用了。。。一定要记得把要用的数据但未更新的初始化,主要是考察当前所要使用数据的范围有无超出更新的范围。
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <set> #include <algorithm> //#define LOCAL using namespace std; map<string, string> mta; struct Mail { string name, addr; Mail(string n = "", string a = ""): name(n), addr(a) {} //要设置默认值,不然会报错 }; inline pair<string, string> parse_mail(string s) { int i = s.find(‘@‘); return make_pair(s.substr(0, i), s.substr(i + 1, s.length() - i - 1)); } Mail recp[10000]; map<string, int> occr; set<string> recoccr; int main() { #ifdef LOCAL freopen("in.in", "r", stdin); freopen("out.txt", "w", stdout); #endif bool f; string t, addr, name, send, sendmta, data, curmta, inden = " "; int n, recn; while (cin >> t && t[0] == ‘M‘) { cin >> addr >> n; for (int i = 0; i < n; ++i) { cin >> name; mta[name] = addr; } } while (cin >> send && send[0] != ‘*‘) { data = ""; int i = send.find(‘@‘); sendmta = send.substr(i + 1, send.length() - i - 1); recn = 0; occr.clear(); recoccr.clear(); while (cin >> t && t[0] != ‘*‘) { pair<string, string> p = parse_mail(t); if (!recoccr.count(t)) recp[recn++] = Mail(p.first, p.second), recoccr.insert(t); //这段代码实在太糟了,但实在不想大改代码了。。 if (!occr.count(p.second)) occr[p.second] = recn; } stable_sort(recp, recp + recn, [] (Mail a, Mail b) { return occr[a.addr] < occr[b.addr]; }); recp[recn] = Mail(); //初始化 getline(cin, t); //把上一行的换行符弄掉,这里很容易忽略 while (getline(cin, t) && t[0] != ‘*‘) { data += inden + t + " "; } data += inden + ". "; curmta = ""; for (int i = 0; i < recn; ++i) { if (recp[i].addr != curmta) { f = false; curmta = recp[i].addr; cout << "Connection between " << sendmta << " and " << curmta << endl; cout << " HELO " << sendmta << " " << " 250 MAIL FROM:<" << send << "> 250 "; } cout << " RCPT TO:<" << recp[i].name << "@" << recp[i].addr << "> "; if (mta[recp[i].name] == recp[i].addr) cout << " 250 ", f = true; else cout << " 550 "; if (recp[i + 1].addr != curmta) // 这里用到了recn这一未更新的数据 { if (f) cout << inden << "DATA " << inden << "354 " << data << inden << "250 "; cout << inden << "QUIT " << inden << "221 "; } } } }
以上是关于The Letter Carrier's Rounds UVA - 814的主要内容,如果未能解决你的问题,请参考以下文章
The Letter Carrier's Rounds UVA - 814
UVA 814 The Letter Carrier's Rounds(JAVA基础map)
ZooKeeper Commands: The Four Letter Words 获取监控状态