poj 1002

 //poj 1002

//沒有AC Time Limit Exceeded ,我用了鏈表,該用堆或二叉樹的,有時間的時候在改下吧。先把我寫的代碼記在這裏
  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <stdlib.h> 
  4. #include <ctype.h> 
  5.  
  6. #define MAX_CHAR 50 
  7. #define STD_CHAR_COUNT 10 
  8. typedef struct node 
  9.     char *data; 
  10.     int count; 
  11.     struct node * next; 
  12. }Node,List; 
  13.  
  14. List * creatList() 
  15.     Node *head = (Node *)malloc(sizeof(Node)); 
  16.     head->next=NULL; 
  17.     return head; 
  18.  
  19. void insertList(List *list,char * data) 
  20.     Node *p = list->next; 
  21.     Node *preNode = list; 
  22.     Node *newNode; 
  23.     int rs; 
  24.     while(p) 
  25.     { 
  26.         rs = strcmp(data,p->data); 
  27.         if (rs==0) 
  28.         { 
  29.             p->count++; 
  30.             return ; 
  31.         } 
  32.         else if (rs>0) 
  33.         { 
  34.             preNode = p; 
  35.             p=p->next; 
  36.         } 
  37.         else 
  38.         { 
  39.             break
  40.         } 
  41.     } 
  42.     newNode = (Node *)malloc(sizeof(Node)); 
  43.     newNode->data = (char *)malloc(STD_CHAR_COUNT*sizeof(char)); 
  44.     memset(newNode->data,0,STD_CHAR_COUNT*sizeof(char)); 
  45.     memcpy(newNode->data,data,strlen(data)); 
  46.     newNode->count = 1; 
  47.     preNode->next = newNode; 
  48.     newNode->next = p; 
  49. void printList(List *list) 
  50.     int count1 =0; 
  51.     List *p = list->next; 
  52.     while(p) 
  53.     { 
  54.         if (p->count>1) 
  55.         { 
  56.             printf("%s %d\n",p->data,p->count); 
  57.             count1++; 
  58.         } 
  59.         p=p->next; 
  60.     } 
  61.     if (count1==0) 
  62.     { 
  63.         printf("No duplicates."); 
  64.     } 
  65. void distoryList(List *list) 
  66.     Node *p,*q; 
  67.     p = list->next; 
  68.     while(p) 
  69.     { 
  70.         q = p->next; 
  71.         free(p->data); 
  72.         free(p); 
  73.         p=q; 
  74.     } 
  75.     free(list); 
  76. int main(void
  77.     int numCount; 
  78.     List *list; 
  79.     char nowStr[MAX_CHAR]; 
  80.     char nowStdNum[STD_CHAR_COUNT]; 
  81.  
  82.     int i,j; 
  83.     int charCount; 
  84.     int stdCharCount; 
  85.     scanf("%d",&numCount); 
  86.     list = creatList(); 
  87.     for (i=0;i<numCount;i++) 
  88.     { 
  89.         memset(nowStr,0,MAX_CHAR*sizeof(char)); 
  90.         memset(nowStdNum,0,STD_CHAR_COUNT*sizeof(char)); 
  91.         scanf("%s",nowStr); 
  92.         stdCharCount=0; 
  93.         charCount = strlen(nowStr); 
  94.         for (j=0;j<charCount;j++) 
  95.         { 
  96.             if (nowStr[j]=='-'
  97.                 continue
  98.             if (isupper(nowStr[j])) 
  99.             { 
  100.                 switch (nowStr[j]) 
  101.                 { 
  102.                 case 'A'case 'B'case 'C': nowStr[j] = '2'break
  103.                 case 'D'case 'E'case 'F': nowStr[j] = '3'break
  104.                 case 'G'case 'H'case 'I': nowStr[j] = '4'break
  105.                 case 'J'case 'K'case 'L': nowStr[j] = '5'break
  106.                 case 'M'case 'N'case 'O': nowStr[j] = '6'break
  107.                 case 'P'case 'R'case 'S': nowStr[j] = '7'break
  108.                 case 'T'case 'U'case 'V': nowStr[j] = '8'break
  109.                 case 'W'case 'X'case 'Y': nowStr[j] = '9'break
  110.                 } 
  111.             } 
  112.             nowStdNum[stdCharCount]=nowStr[j]; 
  113.             stdCharCount++; 
  114.             if (stdCharCount==3) 
  115.             { 
  116.                 nowStdNum[stdCharCount]='-'
  117.                 stdCharCount++; 
  118.             } 
  119.         } 
  120.         insertList(list,nowStdNum); 
  121.     } 
  122.     printList(list); 
  123.     distoryList(list); 
  124.     return 0; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章