Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

900多MS,不知是慢在map还是cout

Posted by tadvent at 2009-08-13 12:05:12 on Problem 1002
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<numeric>
#include<map>
#include<string>
using namespace std;

inline int char2int(const char &ch){
    static int charmap[] = {
        2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9
    };
    return charmap[ch - 'A'];
}
struct charplus{
    int operator()(int left, char right)const{
        if(right >= 'A' && right <= 'Z'){
            return left * 10 + char2int(right);
        } else if(right >= '0' && right <= '9'){
            return left * 10 + right - '0';
        } else return left;
    }
};
inline int str2int(const string& str){
    return accumulate(str.begin(), str.end(), 0, charplus());
}
class numout{
    ostream &o;
    bool hasout;
public:
    numout(ostream &os):o(os), hasout(false){}
    void operator()(const pair<int, int>& p){
        if(p.second > 1){
            hasout = true;
            o<<setw(3)<<p.first/10000<<'-'<<setw(4)<<p.first%10000<<' '<<p.second<<'\n';
        }
    }
    operator void*(){
        return (void*)(hasout? 1:0);
    }
};

int main(){
    int n;
    string line;
    map<int, int> numcnt;

    cin>>n;
    getline(cin, line);
    while(n--){
        getline(cin, line);
        ++numcnt[str2int(line)];
    }

    cout.fill('0');
    if(!for_each(numcnt.begin(), numcnt.end(), numout(cout))){
        cout<<"No duplicates."<<endl;
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator