[刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

8 篇文章 0 订阅

第一次玩ACM。。。有点小紧张小兴奋。这题目好难啊,只是网赛就这么难。。。只把最简单的两题做出来了。

题目1:
这里写图片描述


代码:

//#define _ACM_
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;

char line[111], word[111];
int T(0);
struct CAT{
    //string N;         //name of current category
    map<string, CAT> C; //categorys
    set<string> B;      //books
    void show(const int sp) const{
        for (const auto &r : C) {
            for (int i = 0;i < sp;++i) printf("    ");
            printf("%s\n", r.first.c_str());
            r.second.show(sp+1);
        }
        for (const auto &r : B) {
            for (int i = 0;i < sp;++i) printf("    ");
            printf("%s\n", r.c_str());
        }
    }
};

int main()
{
#ifdef _ACM_
#define gets(T) gets_s(T, 111)
    freopen("in.txt", "r", stdin);
#endif

    while (gets(line)) {
        map<string, CAT> cat;
        do {
            CAT* pc(NULL);
            char *p(word);
            for (char *pl(line);*pl != '\0';++pl) {
                if (*pl != '/') *p++ = *pl;
                else {
                    *p = '\0';p = word;
                    if (pc == NULL) pc = &cat[word];
                    else pc = &(pc->C[word]);
                }
            }
            *p = '\0';
            pc->B.insert(word);
        } while (gets(line) && strcmp(line, "0"));
        printf("Case %d:\n", ++T);
        for (const auto &r : cat) {
            printf("%s\n", r.first.c_str());
            r.second.show(1);
        }
    }
    return 0;
}

题目3:
这里写图片描述


代码:

//#define _ACM_
#include<iostream>
#include<string>
#include<map>
using namespace std;

inline bool isch(char c) { return c >= 'a' && c <= 'z'; }

char ch, line[555], word[555];
string pr;
map<string, unsigned> phs;
int main()
{
#ifdef _ACM_
#define gets(T) gets_s(T, 555)
    freopen("in.txt", "r", stdin);
#endif

    ch = getchar();
    while (true) {
        char *p(word);
        while (isch(ch)) {
            *p++ = ch;
            ch = getchar();
        }
        *p = '\0';
        bool ispunc(false);
        while (ch != EOF && !isch(ch) && ch != '#') {
            if (ch == ',' || ch == '.' || ch == '\n') ispunc = true;
            ch = getchar();
        }
        if (ch == EOF) break;//the end of test file
        else if (ch == '#') {//the end of current text
            if (!pr.empty()) ++phs[pr + ' ' + word];
            if (ispunc) pr.clear();
            else pr = word;
            unsigned maxu = 0;
            string maxs;
            for (const auto& r : phs)
                if (r.second > maxu) maxu = r.second, maxs = r.first;
            printf("%s:%u\n", maxs.c_str(), maxu);
            phs.clear(), pr.clear();
            getchar(), getchar(), getchar(), getchar();
            ch = getchar();
        }
        else {//find a word/phrase
            if (!pr.empty()) ++phs[pr + ' ' + word];
            if (ispunc) pr.clear();
            else pr = word;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值