码迷,mamicode.com
首页 > 其他好文 > 详细

The Letter Carrier's Rounds UVA - 814

时间:2020-01-27 22:15:40      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:turn   txt   style   quit   +=   uva   color   out   经验   

记这题主要是想记录两条经验,一个是要考虑数据的可重性,删去重复数据;二是跟上篇博客一样的错误,数组复写导致数据交叉而引起的奇妙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 + "\n";
        }
        data += inden + ".\n";
        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 << "\n" << "     250\n     MAIL FROM:<" << send << ">\n     250\n";
            }
            cout << "     RCPT TO:<" << recp[i].name << "@" << recp[i].addr << ">\n";
            if (mta[recp[i].name] == recp[i].addr) cout << "     250\n", f = true;
            else cout << "     550\n";  
            if (recp[i + 1].addr != curmta) // 这里用到了recn这一未更新的数据
            {
                if (f) cout << inden << "DATA\n" << inden << "354\n" << data << inden << "250\n";
                cout << inden << "QUIT\n" << inden << "221\n";
            }
        }
    }
}

 

The Letter Carrier's Rounds UVA - 814

标签:turn   txt   style   quit   +=   uva   color   out   经验   

原文地址:https://www.cnblogs.com/jionkitten/p/12236720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!