814 - The Letter Carrier‘s Rounds (UVA)

题目链接如下:

Online Judge

我的代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
// #define debug

struct MTA{
    std::string city;
    std::set name;
};
struct rcpt{
    std::string city;
    std::vector name;
};
char city[16], name[16], s[32];
int n, loc, cnt;
std::string str, s1, s2, msg, senderName, senderCity;
bool flag;
std::map mp;
std::vector mtaVec;
std::string indent = "     ";

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%s", s) == 1 && s[0] != '*'){
        scanf("%s %d", city, &n);
        mp[city] = mtaVec.size();
        mtaVec.resize(mp.size());
        mtaVec.back().city = city;
        while(n--){
            scanf("%s", name);
            mtaVec.back().name.insert(name);
        }
    }
    while(scanf("%s", s) == 1 && s[0] != '*'){
        std::vector vec;
        cnt = 0;
        do{
            str = s;
            loc = str.find('@');
            s1 = str.substr(0, loc);
            s2 = str.substr(loc + 1);
            if(!cnt){
                senderName = s1;
                senderCity = s2;
                ++cnt;
                continue;
            }
            flag = false;
            for(int i = 0; i < vec.size(); ++i){
                if(vec[i].city == s2){
                    flag = true;
                    if(find(vec[i].name.begin(), vec[i].name.end(), s1) == vec[i].name.end()){
                        vec[i].name.push_back(s1);
                    }
                    break;
                }
            }
            if(!flag){
                vec.resize(vec.size() + 1);
                vec.back().city = s2;
                vec.back().name.push_back(s1);
            }
        } while(scanf("%s", s) == 1 && s[0] != '*');
        getchar();
        msg.clear();
        while(getline(std::cin, str) && str[0] != '*'){
            msg += indent + str + "\n";
        }
        msg += indent + ".\n";
        for(int i = 0; i < vec.size(); ++i){
            printf("Connection between %s and %s\n", senderCity.c_str(), vec[i].city.c_str());
            printf("%sHELO %s\n%s250\n%sMAIL FROM:<%s@%s>\n%s250\n", indent.c_str(), senderCity.c_str(), indent.c_str(), indent.c_str(), senderName.c_str(), senderCity.c_str(), indent.c_str());
            flag = false;
            for(int j = 0; j < vec[i].name.size(); ++j){
                printf("%sRCPT TO:<%s@%s>\n", indent.c_str(), vec[i].name[j].c_str(), vec[i].city.c_str());
                if(mtaVec[mp[vec[i].city]].name.find(vec[i].name[j]) != mtaVec[mp[vec[i].city]].name.end()){
                    printf("%s250\n", indent.c_str());
                    flag = true;
                } else{
                    printf("%s550\n", indent.c_str());
                }
            }
            if(flag){
                printf("%sDATA\n%s354\n%s%s250\n", indent.c_str(), indent.c_str(), msg.c_str(), indent.c_str());
            }
            printf("%sQUIT\n%s221\n", indent.c_str(), indent.c_str());
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

写完后看了下刘汝佳的解法,他把name@city直接存到set里,简化了一些步骤。

修改后如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
// #define debug

struct rcpt{
    std::string city;
    std::vector name;
};
char s[32];
int n, loc;
std::string str, s1, s2, msg, senderName, senderCity;
bool flag;
std::set addr;
std::string indent = "     ";

void parseAddr(std::string &ss, std::string &city, std::string &name){
    loc = ss.find('@');
    city = ss.substr(0, loc);
    name = ss.substr(loc + 1);
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%s", s) == 1 && s[0] != '*'){
        scanf("%s %d", s, &n);
        s2 = s;
        while(n--){
            scanf("%s", s);
            s1 = s;
            addr.insert(s1 + "@" + s2);
        }
    }
    while(scanf("%s", s) == 1 && s[0] != '*'){
        str = s;
        parseAddr(str, senderName, senderCity);
        std::vector vec;
        while(scanf("%s", s) == 1 && s[0] != '*'){
            str = s;
            parseAddr(str, s1, s2);
            flag = false;
            for(int i = 0; i < vec.size(); ++i){
                if(vec[i].city == s2){
                    flag = true;
                    if(find(vec[i].name.begin(), vec[i].name.end(), s1) == vec[i].name.end()){
                        vec[i].name.push_back(s1);
                    }
                    break;
                }
            }
            if(!flag){
                vec.resize(vec.size() + 1);
                vec.back().city = s2;
                vec.back().name.push_back(s1);
            }
        }
        getchar();
        msg.clear();
        while(getline(std::cin, str) && str[0] != '*'){
            msg += indent + str + "\n";
        }
        msg += indent + ".\n";
        for(int i = 0; i < vec.size(); ++i){
            printf("Connection between %s and %s\n", senderCity.c_str(), vec[i].city.c_str());
            printf("%sHELO %s\n%s250\n%sMAIL FROM:<%s@%s>\n%s250\n", indent.c_str(), senderCity.c_str(), indent.c_str(), indent.c_str(), senderName.c_str(), senderCity.c_str(), indent.c_str());
            flag = false;
            for(int j = 0; j < vec[i].name.size(); ++j){
                str = vec[i].name[j] + "@" + vec[i].city;
                printf("%sRCPT TO:<%s>\n", indent.c_str(), str.c_str());
                if(addr.count(str)){
                    printf("%s250\n", indent.c_str());
                    flag = true;
                } else{
                    printf("%s550\n", indent.c_str());
                }
            }
            if(flag){
                printf("%sDATA\n%s354\n%s%s250\n", indent.c_str(), indent.c_str(), msg.c_str(), indent.c_str());
            }
            printf("%sQUIT\n%s221\n", indent.c_str(), indent.c_str());
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++,算法)