UVA-814 邮件传输代理的交互 今日竞赛刷题有感

这个题是个模拟类型的题,题目很长,但是读下来很容易理解意思,算法也不难,一顿输入输出就行了,但是却让我耗费了一天的时间来debug。这个题去重很关键,而且在去重的同时还要保证原有输入输出顺序不变,这个倒是容易解决,加个set或者迭代器加algorithm库里的find函数判断一下再插入容器就行,很容易从debug数据中找到。

但是,,,对于我来说最难的是输入和输出。竞赛中的输入输出太重要了,输出不好会造成PE,输入不对则将是无穷无尽的WA!!!这个题一开始我是一行一行读入,但是总是报错,de了一天bug,从算法逻辑到去重都没问题,疑惑的时候看见紫书例程的输入输出是逐个输入,而不是逐行输入,于是将代码输入全部换成了逐个输入,一遍就AC了。。。猜想:有时竞赛数据可能不是像样例一样一行一行输入的,而是本应在一行的数据分成好几行输入的,这个时候只有根据题目的提示(如这题mta后面的那个数字)进行逐个输入。

至于怎么输入输出才准确,我们这些小白只有多做题才能获取经验哈哈。

附上今日代码:

#include<iostream>
#include<unordered_map>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;

unordered_map<string,vector<string>> people2city;//名字对城市的一个哈希表

//构建一个输出信息的函数
void Display_info(string sender_city,string user_name,string recevier_city,vector<string>& recevier_name,vector<string>& message_lines){
    cout<<"Connection between "<<sender_city<<" and "<<recevier_city<<endl;
    //HELO
    cout<<"     HELO "<<sender_city<<endl;
    cout<<"     250"<<endl;
    //MAIL FROM
    cout<<"     MAIL FROM:<"<<user_name<<"@"<<sender_city<<">"<<endl;
    cout<<"     250"<<endl;
    int count=0;//计算成功接收邮件的人数
    //RCPT TO
    for(int i=0;i<recevier_name.size();i++){
        auto it=people2city.find(recevier_name[i]);
        if(it==people2city.end()){
            cout<<"     RCPT TO:<"<<recevier_name[i]<<"@"<<recevier_city<<">"<<endl;
            cout<<"     550"<<endl;
        }
        else{
            auto it1=find(people2city[recevier_name[i]].begin(),people2city[recevier_name[i]].end(),recevier_city);
            if(it1==people2city[recevier_name[i]].end()){
                cout<<"     RCPT TO:<"<<recevier_name[i]<<"@"<<recevier_city<<">"<<endl;
                cout<<"     550"<<endl;
            }
            else{
                count++;
                cout<<"     RCPT TO:<"<<recevier_name[i]<<"@"<<recevier_city<<">"<<endl;
                cout<<"     250"<<endl;
            }
        }
    }
    //如果接收人数大于0,就必须发送信息
    if(count>0){
        cout<<"     DATA"<<endl;
        cout<<"     354"<<endl;
        //发送正文
        for(int k=0;k<message_lines.size();k++){
            cout<<"     "<<message_lines[k]<<endl;
        }
        cout<<"     ."<<endl;
        cout<<"     250"<<endl;
    }
    cout<<"     QUIT"<<endl;
    cout<<"     221"<<endl;
    return;
}

int main(){
    int star_num=0;
    string s,t;
    int k;
    while(cin>>s&&s!="*"){
        cin>>s>>k;
        while(k--){
            cin>>t;
            auto it=find(people2city[t].begin(),people2city[t].end(),s);
            if(it==people2city[t].end()){
                people2city[t].push_back(s);
            }
        }
    }
    while(cin>>s&&s!="*"){
        unordered_map<string,vector<string>> city2recevier_name;
        vector<string> city;
        vector<string> messages_lines;
        string sender_city="";
        string sender_name="";
        //发件人
        stringstream sin(s);
        string tok;
        getline(sin,tok,'@');
        sender_name=tok;
        getline(sin,tok,'@');
        sender_city=tok;
        //收件人
        while(cin>>t&&t!="*"){
            //cout<<t<<endl;
            stringstream sin1(t);
            string tok1;
            string user_name;
            string user_city;
            getline(sin1,tok1,'@');
            user_name=tok1;
            getline(sin1,tok1,'@');
            user_city=tok1;

            auto it=find(city.begin(),city.end(),user_city);
            if(it==city.end()){city.push_back(user_city);}
            auto it1=find(city2recevier_name[user_city].begin(),city2recevier_name[user_city].end(),user_name);
            if(it1==city2recevier_name[user_city].end()){city2recevier_name[user_city].push_back(user_name);}
        }
        getline(cin,t);//吃回车
        string lines;
        //cout<<city[0]<<endl;
        while(getline(cin,lines)&&lines!="*"){
            messages_lines.push_back(lines);
        }
        for(int i=0;i<city.size();i++){
            Display_info(sender_city,sender_name,city[i],city2recevier_name[city[i]],messages_lines);
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值