北大acm1002

这道题开始用链表来做,遍历之后有序插入,经常new对象,而且一开始next指针没有设置为null导致了一直runtime error,后来发现这个问题后,就很悲催的Time Limit Exceeded,可能是遍历链表的缘故。

后来猛然发现map这个好东西,会自动按照key的字典序排序,直接输出就好,实在是好东西啊!

但是用g++提交还是超时,用c++确是不超的,原因不知道。。。

题目链接:http://poj.org/problem?id=1002

Source Code

Problem: 1002 User: yuanting0505
Memory: 5200K Time: 1141MS
Language: C++ Result: Accepted
    • Source Code
#include <iostream>
#include <map>
#include <string>
using namespace std;

map <string,int> tel;//号码映射到次数
int num;
char change[]={'2','2','2','3','3','3','4','4','4',
    '5','5','5','6','6','6','7','Q','7','7','8','8','8','9','9','9','Z'};


void input_tel(int n)
{
    for(int i=0;i<n;i++)
    {
        string s;
        string store(8,' ');
        cin>>s;
        int k=0;
        for(int j=0;j<s.length();j++)
        {
            //int k=0;
            if(k==3)
            {
                store[k]='-';
                k++;
                j--;
            }
            else if(s[j]>='A'&&s[j]<='Z')
            {
                store[k]=change[s[j]-'A'];
                k++;
            }
            else if(s[j]=='-')
            {
                continue;
            }
            else
            {
                store[k]=s[j];
                k++;
            }
        }
        tel[store]++;//没有排序,但map按key自动排序
    }

}

void output_tel()
{
    map<string,int>::iterator iter;
    bool dup=false;
    for(iter=tel.begin();iter!=tel.end();iter++)
    {
        if(iter->second>1)
        {
            cout<<iter->first<<' '<<iter->second<<endl;
            dup=true;
        }
    }
    if(dup==false)
    {
        cout<<"No duplicates."<<endl;
    }
    
}

int main(int argc, const char * argv[])
{
    cin>>num;
    input_tel(num);
    output_tel();

    return 0;
}

转载于:https://www.cnblogs.com/yuanting0505/p/3224710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值