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 |
使用C++ STL vector和map容器超时,求分析#include <iostream> #include <vector> #include <map> char parse(const char &ch) { switch(ch){ case '2': case 'A': case 'B': case 'C': return '2'; case '3': case 'D': case 'E': case 'F': return '3'; case '4': case 'G': case 'H': case 'I': return '4'; case '5': case 'J': case 'K': case 'L': return '5'; case '6': case 'M': case 'N': case 'O': return '6'; case '7': case 'P': case 'R': case 'S': return '7'; case '8': case 'T': case 'U': case 'V': return '8'; case '9': case 'W': case 'X': case 'Y': return '9'; case '0': return '0'; case '1': return '1'; default: return '\0'; } } int main( int argc, char *argv[] ) { using namespace std; vector<string> telebook; map<string, int> entries; int total_count; cin >> total_count; for( int i = 0; i < total_count; ++i ) { string temp; cin >> temp; telebook.push_back(temp); } vector<string>::iterator it; for( it = telebook.begin(); it != telebook.end(); ++it ) { string temp; char ch; map<string, int>::iterator map_it; for( int i = 0; i < it->length(); ++i ) { if( ( ch = parse(it->at(i)) ) != '\0' ) temp.push_back(ch); } if( (map_it = entries.find(temp)) != entries.end() ) { (map_it->second)++; } else { entries.insert( pair<string, int>(temp, 1) ); } } if( entries.size() == total_count ) cout << "No duplicates." << endl; else { map<string, int>::iterator m_it; for( m_it = entries.begin(); m_it != entries.end(); ++m_it ) { if( m_it->second != 1 ) { string temp(m_it->first); temp.insert(3, "-"); cout << temp << " " << m_it->second << 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