| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
花了2+个小时终于AC了,无奈注意:
1.处理好单词之间只有标点没有空格的情况
2.注意检测文件尾
实现方法:STL
附上代码
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int max = 0;
void u(map <string,int> &m,string &word){
string no = ",.;\'`\"()/:- ";
string :: iterator iter = word.begin(),ii = word.begin();
int b = 0,e = 0;
string :: size_type pos = 0;
while(true){
while(iter != word.end() && no.find(*iter,pos) == string :: npos){
*iter = tolower(*iter);
++e;
++iter;
}
string tmp(ii,ii + e - b);
if(tmp != "")
m[tmp] ++;
if(::max < m[tmp])
::max = m[tmp];
if(iter == word.end())
break;
ii = ++ iter;
b = ++ e;
}
return;
}
int main(){
string word;
vector<string>last;
map <string,int>total;
// ifstream fin("input.txt");
// ofstream fout("output.txt");
while(cin >> word){
u(total,word);
//char ch = fin.peek();
char ch = cin.peek();
if(ch == '\n' || ch == ' ')
cin.get();
}
cout << ::max << " occurrences" << endl;
map<string,int>::iterator iter = total.begin();
while(iter != total.end()){
if(iter -> second == ::max)
//fout << iter -> first << endl;
last.push_back(iter -> first);
++iter;
}
sort(last.begin(),last.end());
for(int i = 0 ; i < last.size() ; i ++){
//fout << last[i] << endl;
cout << last[i] << endl;
}
// system("pause");
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator