栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1747 Charity Begins at Ho...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1747 Charity Begins at Ho...

#include <set>#include <map>#include <cctype>#include <cstdio>#include <string>#include <algorithm>using namespace std;const int SS = 1, MM = 60 * SS, HH = 60 * MM, DD = 24 * HH, TEN = 10;const int DIFF = 6 * HH;inline int CharToInt(const char ch){    return ch - '0';}inline int TimeToInt(const char* const Time){    return  CharToInt(Time[0]) * TEN * DD + CharToInt(Time[1]) * DD +        CharToInt(Time[3]) * TEN * HH + CharToInt(Time[4]) * HH +        CharToInt(Time[6]) * TEN * MM + CharToInt(Time[7]) * MM +        CharToInt(Time[9]) * TEN * SS + CharToInt(Time[10]) * SS;}inline void ToLower(string& str){    for (size_t i = 0; i < str.length(); i++)        if(isupper(str[i])) str[i] = tolower(str[i]);}struct Address{    const string topDomain;    const string Domain;    map<string, pair<int, pair<int, int> > >* const Records;    Address(const string& atopDomain, const string& aDomain) : topDomain(atopDomain), Domain(aDomain),        Records(new map<string, pair<int, pair<int ,int> > >)    {    }    ~Address()    {        delete Records;    }    bool operator<(const Address& rhs) const    {        if(topDomain == rhs.topDomain) return Domain < rhs.Domain;        else return topDomain < rhs.topDomain;    }    void insert(const string& cur, const int atime) const    {        map<string, pair<int, pair<int, int> > >::iterator it = Records->find(cur);        if(it == Records->end()) (*Records)[cur] = make_pair(atime, make_pair(1, 0));        else if(atime < it->second.first + DIFF) ++it->second.second.second;        else { ++it->second.second.first; it->second.first = atime;        }    }    void show() const    {        printf("Second level domain = %sn", Domain.c_str());        for (map<string, pair<int, pair<int, int> > >::const_iterator it = Records->begin(); it != Records->end(); ++it) printf("%-65s%4d%4dn", it->first.c_str(), it->second.second.first, it->second.second.second);    }};char buf[88];int main(void){    string::size_type pre, pos;    int atime;    string cur, topDomain, Domain;    Address *add;    set<Address> st;    set<Address>::iterator it;    while(gets(buf)) {        if(buf[0] == '3' && buf[1] == '2') break;        atime = TimeToInt(buf);        cur = string(buf + 12);        ToLower(cur);        pre = cur.rfind('.');        topDomain = cur.substr(pre + 1);        pos = cur.rfind('.', pre - 1);        if(pos == string::npos) pos = cur.rfind('@', pre - 1);        else pos = max(pos, cur.rfind('@', pre - 1));         Domain = cur.substr(pos + 1, pre - pos - 1);        add = new Address(topDomain, Domain);        it = st.find(*add);        if(it == st.end()) { st.insert(*add); it = st.find(*add);        }        else { delete add; add = NULL;        }        it->insert(cur, atime);    }    topDomain = "";    for (it = st.begin(); it != st.end(); ++it) {        if(it->topDomain != topDomain) { topDomain = it->topDomain; if(it != st.begin())     puts("n"); printf("TOP-LEVEL DOMAIN = %sn", topDomain.c_str());        }        else if(it != st.begin()) puts("");        it->show();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号