Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
指针链表 time limit exceeded 还有高手看这题吗????帮帮帮忙#define LEN sizeof(struct key) #include<ctype.h> #include<stdio.h> #include<stdlib.h> struct key{ char code[8]; int m; struct key *next; }; struct key*insert(struct key*head,char co[]) { struct key *p,*p1,*p2; if(head==NULL) { head=(struct key*)malloc(LEN); strcpy(head->code,co); head->m=1; head->next=NULL; return head; } p2=p1=head; p=(struct key *)malloc(LEN); strcpy(p->code,co); p->m=1; while(strcmp(p->code,p1->code)>0&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(strcmp(p->code,p1->code)==0) {(p1->m)++;free(p);} else if(strcmp(p->code,p1->code)<0) { if(head==p1)head=p; else p2->next=p; p->next=p1; } else{ p1->next=p; p->next=NULL; } return(head); } void list(struct key*head) { struct key*p; int i,flag=1; p=head; while(p!=NULL) { if(p->m>=2) { flag=0; for(i=0;i<=2;i++) printf("%c",p->code[i]); printf("-"); for(i=3;i<=6;i++) printf("%c",p->code[i]); printf(" %d\n",p->m); } p=p->next; } if(flag)printf("No duplicates."); } main() { char c,code1[8]={'\0'}; int con[]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','7','7','7','8','8','8','9','9','9'}; int n,i,j; struct key *head; scanf("%d",&n); getchar(); head=NULL; for(i=0;i<n;i++) { j=0; scanf("%c",&c); while(c!='\n') { if((c>='A'&&c<='P')||(c>='R'&&c<='Y')||(c<='9'&&c>='0')) if(isalpha(c)) { code1[j]=con[c-'A']; j++; } else { code1[j]=c; j++; } scanf("%c",&c); } code1[j]='\0'; head=insert(head,code1); } list(head); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator