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 |
典型水题,不过偷懒用了STL效率略低,16ms#include <iostream> #include <stdio.h> #include <string> #include <map> #include <algorithm> #include <vector> using namespace std; char toL(char c){ if((c>='a' && c<='z') || (c>='0' && c<='9')) return c; if(c>='A' && c<='Z') return c-'A'+'a'; return '\0'; } int main() { int mx = 0; map<string, int> sim; bool inWord = false; string tmpWord; int nc; while((nc = getchar()) != EOF){ if(!inWord){ char tol = toL((char)nc); if(tol){ inWord = true; tmpWord += tol; } } else{ char tol = toL((char)nc); if(tol){ tmpWord += tol; } else{ inWord = false; map<string,int>::iterator it = sim.find(tmpWord); if(it != sim.end()){ it->second++; if(mx < it->second) mx = it->second; } else{ sim.insert(pair<string,int>(tmpWord,1)); if(!mx) mx = 1; } tmpWord = ""; } } } if(inWord){ map<string,int>::iterator it = sim.find(tmpWord); if(it != sim.end()){ it->second++; if(mx < it->second) mx = it->second; } else{ sim.insert(pair<string,int>(tmpWord,1)); if(!mx) mx = 1; } } vector<string> vs; for(map<string,int>::iterator it = sim.begin(); it != sim.end(); it++){ if(it->second == mx){ vs.push_back(it->first); } } sort(vs.begin(), vs.end()); cout << mx << " occurrences" << endl; int sz = vs.size(); for(int i = 0; i < sz; i++){ cout << vs[i] << endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator