思考:
1、别忘了清空数组等
2、想好了再写
代码写得比较冗余。其实主要代码段就是
int l = strlen (s);
for (int i = 0; i <= l; i++) {
if (s[i] == ',' || s[i] == '.' || s[i] == '\0') {
if (flag1 == 2) {
string s2 = first;
s2 += ' ';
s2 += second;
//cout << first << " " << second << " " << s2 << endl;
//cout << flag1 << " " << flag2 << endl;
if (mp.count (s2)) {
mp[s2]++;
} else {
mp[s2] = 1;
}
first.clear();
second.clear();
maxnum = max (maxnum, mp[s2]);
}
first.clear();
flag1 = 1;
flag2 = 1;
} else if (s[i] == ' ') {
if (flag2) continue;
if (i != 0 && s[i - 1] == ' ') continue;
if (flag1 == 1) {
flag1 = 2;
} else if (flag1 == 2) {
string s2 = first;
s2 += ' ';
s2 += second;
//cout << s2 << endl;
//cout << flag1 << " " << flag2 << endl;
if (mp.count (s2)) {
mp[s2]++;
} else {
mp[s2] = 1;
}
first = second;
second.clear();
maxnum = max (maxnum, mp[s2]);
}
} else {
flag2 = 0;
if (flag1 == 1) first += s[i];
if (flag1 == 2) second += s[i];
}
}
用Map存储所有的短语出现过的次数,最后扫一遍找出现最多次的短语就好了。
上面的哪一段代码实现的功能是把所有的短语都加入到map里。这道题是纯模拟,比较麻烦,不过处理好了短语之后,也就好做多了。
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
const int maxn = 600;
map<string, int> mp;
int main()
{
#ifndef ONLINE_JUDGE
freopen ("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
char s[maxn];
while (gets (s)) {
int maxnum = 0;
mp.clear();
int flag1 = 1;
int flag2 = 1;
string first, second;
first.clear();
second.clear();
int l = strlen (s);
for (int i = 0; i <= l; i++) {
if (s[i] == ',' || s[i] == '.' || s[i] == '\0') {
if (flag1 == 2) {
string s2 = first;
s2 += ' ';
s2 += second;
//cout << first << " " << second << " " << s2 << endl;
//cout << flag1 << " " << flag2 << endl;
if (mp.count (s2)) {
mp[s2]++;
} else {
mp[s2] = 1;
}
first.clear();
second.clear();
maxnum = max (maxnum, mp[s2]);
}
first.clear();
flag1 = 1;
flag2 = 1;
} else if (s[i] == ' ') {
if (flag2) continue;
if (i != 0 && s[i - 1] == ' ') continue;
if (flag1 == 1) {
flag1 = 2;
} else if (flag1 == 2) {
string s2 = first;
s2 += ' ';
s2 += second;
//cout << s2 << endl;
//cout << flag1 << " " << flag2 << endl;
if (mp.count (s2)) {
mp[s2]++;
} else {
mp[s2] = 1;
}
first = second;
second.clear();
maxnum = max (maxnum, mp[s2]);
}
} else {
flag2 = 0;
if (flag1 == 1) first += s[i];
if (flag1 == 2) second += s[i];
}
}
while (gets (s)) {
if (s[0] == '#') break;
int flag1 = 1;
int flag2 = 1;
string first, second;
int l = strlen (s);
for (int i = 0; i <= l; i++) {
if (s[i] == ',' || s[i] == '.' || s[i] == '\0') {
if (flag1 == 2) {
string s2 = first;
s2 += ' ';
s2 += second;
//cout << s2 << endl;
//cout << flag1 << " " << flag2 << endl;
if (mp.count (s2)) {
mp[s2]++;
} else {
mp[s2] = 1;
}
first.clear();
second.clear();
maxnum = max (maxnum, mp[s2]);
}
first.clear();
flag1 = 1;
flag2 = 1;
} else if (s[i] == ' ') {
if (flag2) continue;
if (i != 0 && s[i - 1] == ' ') continue;
if (flag1 == 1) {
flag1 = 2;
} else if (flag1 == 2) {
string s2 = first;
s2 += ' ';
s2 += second;
// cout << s2 << endl;
// cout << flag1 << " " << flag2 << endl;
if (mp.count (s2)) {
mp[s2]++;
} else {
mp[s2] = 1;
}
first = second;
second.clear();
maxnum = max (maxnum, mp[s2]);
}
flag2 = 1;
} else {
flag2 = 0;
if (flag1 == 1) first += s[i];
if (flag1 == 2) second += s[i];
}
}
}
map<string, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++) {
if (it->second == maxnum) {
cout << it->first << ":" << maxnum << endl;
break;
}
}
}
return 0;
}