POJ 1002 487-3279(Map)

可以用Map來求解,很重要的一點就是C++的Map會按照key的字典序來自動排列數據。


這樣就大大簡化了,至少少了排序的一步。


一開始我用的是Map<string, int> ,不得不說轉換成數字存儲的時候是比較簡單,但是也慢多了。顯然就出現了TLE。


然後換,換成 map<int, int> 的,通過乘,來累計得到數字。


但是還是WA。


原來是0000000的問題。


用printf 限制一下,這樣才能通過。


最後AC  Memory : 3976K   Time : 829MS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
map<int,int> num;
map<int,int>::iterator iter;
char s[4000];
char tem[4000];
int st(char ch)
{
    if(ch=='A' || ch=='B' || ch=='C')
        return 2;
    if(ch=='D' || ch=='E' || ch=='F')
        return 3;
    if(ch=='G' || ch=='H' || ch=='I')
        return 4;
    if(ch=='J' || ch=='K' || ch=='L')
        return 5;
    if(ch=='M' || ch=='N' || ch=='O')
        return 6;
    if(ch=='P' || ch=='R' || ch=='S')
        return 7;
    if(ch=='T' || ch=='U' || ch=='V')
        return 8;
    if(ch=='W' || ch=='X' || ch=='Y')
        return 9;
}
int main()
{
    int N;
    scanf("%d",&N);
    for(int i = 0; i<N; ++i)
    {
        scanf("%s",tem);
        int x = 0;
        for(int j=0; tem[j]!='\0'; ++j)
        {
            if(tem[j]=='-' || tem[j]=='Q' || tem[j]=='Z')
                    continue;
            else if(tem[j]<='9')
                    x=x*10+tem[j]-'0';
            else if(tem[j]<='Z')
                    x=x*10+st(tem[j]);
        }
        ++num[x];
    }
    bool have = false;
    for(iter = num.begin(); iter!=num.end(); iter++)
    {
        if(iter->second > 1)
        {
            have = true;
            int k1 = (int)iter->first / 10000;
            int k2 = (int)iter->first %10000;
            printf("%03d-%04d",k1,k2);
            printf(" %d\n",iter->second);
        }
    }
    if(!have)
        cout<<"No duplicates."<<endl;
    return 0;
}


代碼:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章