第一次玩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; }转载于:https://www.cnblogs.com/xienaoban/p/6798084.html
相关资源:数据结构—成绩单生成器