现在的位置: 首页 > 综合 > 正文

POJ 1002 487-3279(Map)

2018年05月02日 ⁄ 综合 ⁄ 共 1208字 ⁄ 字号 评论关闭

可以用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;
}

代码:

抱歉!评论已关闭.