zoj - 3700的题解

zoj - 3700

巨恶心的模拟题,,

题意: 记录每个单词出现的次数,注意,,这里I'd的情况I和d会记录成两个单词,,在这里wa到怀疑人生,,,

按次数分组后从大到小输出每个组里最长的单词

如果有两个相同长度的合法单词,那么他们按字典序排序后输出倒数第二的单词;

直接模拟,,注意细节, 切记检查边界条件;

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<cstdio>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

inline bool check(char &x){
if(x >= 'A' && x <= 'Z')x -= 'A' - 'a';
return (x >= 'a' && x <= 'z');
}

inline bool cmp(string a, string b){
if(a.length() != b.length())return a.length() > b.length();
else{
int le = a.length();
for(int i = 0; i < le; i++){
if(a[i] < b[i])return true;
else if(a[i] > b[i])return false;
}
}
return false;
}

int main()
{
int T;
scanf("%d", &T);
while(T--){
vector<string>ans[5500];
bool cz[5500];
map<string, int>m;
memset(cz, 0, sizeof(cz));
int n;
scanf("%d", &n);
getchar();
string tem;
while(n--){
getline(cin, tem);
if(tem.empty())continue;
istringstream ss(tem);
string s;
while(ss >> s){
while(!s.empty() && !check(s[0]))s.erase(0, 1);
if(s.empty())continue;
int le = s.length();
while(!s.empty() && !check(s[le - 1])){
s.erase(le - 1, 1);
le = s.length();
}
if(s.empty())continue;
bool is = true;
le = s.length();
string tt;
for(int i = 0; i < le; i++){
if(check(s[i]))tt += s[i];
else{
m[tt]++;
tt.clear();
}
}
m[tt]++;
}
}
if(m.empty()){
putchar('\n');
continue;
}
map<string, int>::iterator it = m.begin();
for(; it != m.end(); it++){
ans[it->second].push_back(it->first);
cz[it->second] = true;
}
bool sc = false;
for(int i = 5000; i > 1; i--){
if(cz[i]){
sort(ans[i].begin(), ans[i].end(), cmp);
if(ans[i].size() == 1 || ans[i][0].length() > ans[i][1].length()){
if(sc)putchar(' ');
else sc = true;
cout<< ans[i][0];
}
else{
int now = 1;
int mm = ans[i].size();
while(now < mm - 1 && ans[i][now].length() == ans[i][now + 1].length())now++;//起初没第一个条件,,re到怀疑人生,,
if(sc)putchar(' ');
else sc = true;
cout<< ans[i][now - 1];
}
}
}
putchar('\n');
}

return 0;
}

Comments

2019-03-27

⬆︎TOP