//poj 1002
//沒有AC Time Limit Exceeded ,我用了鏈表,該用堆或二叉樹的,有時間的時候在改下吧。先把我寫的代碼記在這裏
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define MAX_CHAR 50
- #define STD_CHAR_COUNT 10
- typedef struct node
- {
- char *data;
- int count;
- struct node * next;
- }Node,List;
- List * creatList()
- {
- Node *head = (Node *)malloc(sizeof(Node));
- head->next=NULL;
- return head;
- }
- void insertList(List *list,char * data)
- {
- Node *p = list->next;
- Node *preNode = list;
- Node *newNode;
- int rs;
- while(p)
- {
- rs = strcmp(data,p->data);
- if (rs==0)
- {
- p->count++;
- return ;
- }
- else if (rs>0)
- {
- preNode = p;
- p=p->next;
- }
- else
- {
- break;
- }
- }
- newNode = (Node *)malloc(sizeof(Node));
- newNode->data = (char *)malloc(STD_CHAR_COUNT*sizeof(char));
- memset(newNode->data,0,STD_CHAR_COUNT*sizeof(char));
- memcpy(newNode->data,data,strlen(data));
- newNode->count = 1;
- preNode->next = newNode;
- newNode->next = p;
- }
- void printList(List *list)
- {
- int count1 =0;
- List *p = list->next;
- while(p)
- {
- if (p->count>1)
- {
- printf("%s %d\n",p->data,p->count);
- count1++;
- }
- p=p->next;
- }
- if (count1==0)
- {
- printf("No duplicates.");
- }
- }
- void distoryList(List *list)
- {
- Node *p,*q;
- p = list->next;
- while(p)
- {
- q = p->next;
- free(p->data);
- free(p);
- p=q;
- }
- free(list);
- }
- int main(void)
- {
- int numCount;
- List *list;
- char nowStr[MAX_CHAR];
- char nowStdNum[STD_CHAR_COUNT];
- int i,j;
- int charCount;
- int stdCharCount;
- scanf("%d",&numCount);
- list = creatList();
- for (i=0;i<numCount;i++)
- {
- memset(nowStr,0,MAX_CHAR*sizeof(char));
- memset(nowStdNum,0,STD_CHAR_COUNT*sizeof(char));
- scanf("%s",nowStr);
- stdCharCount=0;
- charCount = strlen(nowStr);
- for (j=0;j<charCount;j++)
- {
- if (nowStr[j]=='-')
- continue;
- if (isupper(nowStr[j]))
- {
- switch (nowStr[j])
- {
- case 'A': case 'B': case 'C': nowStr[j] = '2'; break;
- case 'D': case 'E': case 'F': nowStr[j] = '3'; break;
- case 'G': case 'H': case 'I': nowStr[j] = '4'; break;
- case 'J': case 'K': case 'L': nowStr[j] = '5'; break;
- case 'M': case 'N': case 'O': nowStr[j] = '6'; break;
- case 'P': case 'R': case 'S': nowStr[j] = '7'; break;
- case 'T': case 'U': case 'V': nowStr[j] = '8'; break;
- case 'W': case 'X': case 'Y': nowStr[j] = '9'; break;
- }
- }
- nowStdNum[stdCharCount]=nowStr[j];
- stdCharCount++;
- if (stdCharCount==3)
- {
- nowStdNum[stdCharCount]='-';
- stdCharCount++;
- }
- }
- insertList(list,nowStdNum);
- }
- printList(list);
- distoryList(list);
- return 0;
- }