poj P1002错误代码-超时

Run IDUserProblemResultMemoryTimeLanguageCode LengthSubmit Time
103365755565gong1002Time Limit ExceededC++2368B2012-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;
    }
}

链表,速度不够啊~

转载于:https://my.oschina.net/gongshang/blog/63415

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值