zoukankan      html  css  js  c++  java
  • 北大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;
    }

  • 相关阅读:
    Codeforces 754A Lesha and array splitting (搜索)
    浅入分析Linux
    MakeFile基本使用
    Mac 安装YCM
    Homebrew 配置
    虚拟机复制操作CentOS6导致eth0转为eth0以至于网络服务启动失败的解决方案
    Kickstart安装
    Linux编译安装MySQL
    Python源码读后小结
    编译原理小结
  • 原文地址:https://www.cnblogs.com/yuanting0505/p/3224710.html
Copyright © 2011-2022 走看看