poj P1002错误代码-超时

Run ID User Problem Result Memory Time Language Code Length Submit Time
10336575 5565gong 1002 Time Limit Exceeded C++ 2368B 2012-06-21 21:52:11

#include <iostream>
#include <string>
using namespace std;

typedef struct Pnum{
       char numbers[10];
       int times;
       struct Pnum *next;
}Phone_num;

int comp(char str1[], char str2[]){
    char *sp1, *sp2;
    sp1 = str1; 
    sp2 = str2;
    
    while (*sp1 != '\0' && *sp1 == *sp2){
          sp1++;sp2++;
    }
    return *sp1 - *sp2;
};

void run(char s[], Phone_num* first){
     const char map[]={'2','2','2','3','3','3','4','4','4','5','5','5',
           '6','6','6','7','-','7','7','8','8','8','9','9','9','-'};
     char numbers[10]="";
     Phone_num *ptrPnum, *tempPtr, *prePtr;
     int j=0;

     for (int i=0; s[i]!='\0'; ++i ){
         if (s[i]=='-') continue;
         if (s[i]>='A' && s[i]<='Z') {
            numbers[j] = map[s[i]-'A'];
         }
         if (s[i]<'A'){
            numbers[j] = s[i];
         }
         j++;
     }
     numbers[j] == '\0';
     
     prePtr = first;
     ptrPnum = first->next;
     while ((ptrPnum != NULL) && (comp(ptrPnum->numbers, numbers)) <0 ){
           prePtr = ptrPnum;
           ptrPnum = ptrPnum->next;      
     }
     if (ptrPnum != NULL && !comp(ptrPnum->numbers, numbers)){
           ptrPnum->times ++;
     }else {
           tempPtr = (Phone_num *)malloc(sizeof(Phone_num));
           tempPtr-> times = 1;
           memcpy(tempPtr-> numbers, numbers, 10); 
           tempPtr->next = ptrPnum;
           
           prePtr->next = tempPtr;
     }
     //cout<<numbers<<endl;
};

int main(){
    int n;
    char s[50];
    
    cin>>n;
    Phone_num* first, *tmp;
    
    //malloc and init.
    first = (Phone_num *)malloc(sizeof(Phone_num));
    first->next = NULL;
    first->times = 0;
    
    for (int i=0; i<n; ++i){
        cin>>s;
        run(s, first);
    }
    
    tmp = first->next;
    int empty = 1;  // No dumplication.
    while ( tmp != NULL ){
        if (tmp->times > 1){
           empty = 0;
           for (int i = 0; i < 3; i++){
               cout<<tmp->numbers[i];
           }
           cout<<"-";
           for (int i = 3; i < 7; i++){
               cout<<tmp->numbers[i];
           }
           cout<<" "<<tmp->times<<endl;
        }
        tmp = tmp->next;
    }
    if (empty){
       cout<<"No duplicates."<<endl;
    }
    
    //free the memory
    while (first != NULL){
          tmp = first-> next;
          free(first);
          first = tmp;
    }
}

链表,速度不够啊~

你可能感兴趣的:(P1002)