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

Re:以下代码超时,可否优化?

Posted by jyf at 2011-04-19 16:53:03 on Problem 1002
In Reply To:以下代码超时,可否优化? Posted by:jyf at 2011-04-19 16:39:28
/*
如果输入全部一样,则这样输出
printf("No duplicates.\n");
*/
#include<iostream>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#define MAXSIZE 100
using namespace std;
int main(void)
{
    int n;
    scanf("%d",&n);
    char str[MAXSIZE];
    string s;
    map<string,int> m;
    char ch[MAXSIZE]="222333444555666777888999";
    while(n--)
    {
        scanf("%s",str);
        int length=strlen(str);
        int count=0;
        s="";
        for(int i=0;i<length;i++)
        {
            if(str[i]=='-')
            {
                continue;
            }
            else if(str[i]>='A'&&str[i]<='Y')
            {
                /*
                A, B, and C map to 2
                D, E, and F map to 3
                G, H, and I map to 4
                J, K, and L map to 5
                M, N, and O map to 6
                P, R, and S map to 7
                T, U, and V map to 8
                W, X, and Y map to 9
                注意无Q,Z
                */
                if(str[i]>='R'&&str[i]<='Y')
                {
                    s+=ch[str[i]-'A'-1];
                }
                else
                {
                    s+=ch[str[i]-'A'];
                }
            }
            else
            {
                s+=str[i];
            }
            count++;
            if(count==3)
            {
                s+='-';
            }
        }
        m[s]++;
    }
    int state=0;
    for(map<string,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>1)
        {
            state=1;
            printf("%s %d\n",it->first.c_str(),it->second);
            //cout<<it->first<<' '<<it->second<<endl;
        }
    }
    if(state==0)
    {
        printf("No duplicates.\n");
    }
    //system("pause");
    return 0;
}
/*
input
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
output
310-1010 2
487-3279 4
888-4567 3
*/

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