0%

ACMICPC 2016北京赛站网络赛 第1题 第3题

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

题目1: 这里写图片描述


代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//#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: 这里写图片描述


代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//#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;
}