HDU 1880(魔咒词典)解题纠错
2011-03-17 14:03
495 查看
以下代码来自http://topic.csdn.net/u/20110308/20/19cacccd-8c0f-4d08-a902-43fd07f2c087.html
WA的原因是在输出“魔咒”时没有去除括号。以下是修改后AC的代码:
]#include<stdio.h> #include<stdlib.h> #include<string.h> #define max 100005 typedef struct Node { char mo[25]; char yu[85]; }Node; Node mag1[max]; Node mag2[max]; int cmp1(const void *a,const void *b) { Node *c=(Node *)a; Node *d=(Node *)b; return strcmp(c->mo,d->mo); } int cmp2(const void *a,const void *b) { Node *c=(Node *)a; Node *d=(Node *)b; return strcmp(c->yu,d->yu); } int search(char str[],int len,int tag) //采用二分查找,返回查找到的元素在队列中的位置 { int be=0,en=len-1,t; while(be<=en) { t=(be+en)/2; if(tag==0) { if(strcmp(str,mag1[t].mo)<0) en=t-1; else if(strcmp(str,mag1[t].mo)>0) be=t+1; else return t; } else { if(strcmp(str,mag2[t].yu)<0) en=t-1; else if(strcmp(str,mag2[t].yu)>0) be=t+1; else return t; } } return -1; } int main() { char str[120]; int count=0,f; int i; while(gets(str)) { if(strcmp(str,"@END@")==0) break; for(i=0;i<strlen(str);i++) if(str[i]==']') break; strncpy(mag1[count].mo,str,i+1); strcpy(mag1[count].yu,str+i+2); strcpy(mag2[count].mo,mag1[count].mo); strcpy(mag2[count].yu,mag1[count].yu); count++; } qsort(mag1,count,sizeof(Node),cmp1); qsort(mag2,count,sizeof(Node),cmp2); int n; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { gets(str); if(str[0]=='[') { f=search(str,count,0); if(f>=0) printf("%s/n",mag1[f].yu); else printf("what?/n"); } else { f=search(str,count,1); if(f>=0) printf("%s/n",mag2[f].mo); else printf("what?/n"); } } return 0; }
WA的原因是在输出“魔咒”时没有去除括号。以下是修改后AC的代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define max 100005 typedef struct Node { char mo[25]; char yu[85]; }Node; Node mag1[max]; Node mag2[max]; int cmp1(const void *a,const void *b) { Node *c=(Node *)a; Node *d=(Node *)b; return strcmp(c->mo,d->mo); } int cmp2(const void *a,const void *b) { Node *c=(Node *)a; Node *d=(Node *)b; return strcmp(c->yu,d->yu); } int search(char str[],int len,int tag) //采用二分查找,返回查找到的元素在队列中的位置 { int be=0,en=len-1,t; while(be<=en) { t=(be+en)/2; if(tag==0) { if(strcmp(str,mag1[t].mo)<0) en=t-1; else if(strcmp(str,mag1[t].mo)>0) be=t+1; else return t; } else { if(strcmp(str,mag2[t].yu)<0) en=t-1; else if(strcmp(str,mag2[t].yu)>0) be=t+1; else return t; } } return -1; } int main() { char str[120]; int count=0,f; int i; while(gets(str)) { if(strcmp(str,"@END@")==0) break; for(i=0;i<strlen(str);i++) if(str[i]==']') break; strncpy(mag1[count].mo,str,i+1); strcpy(mag1[count].yu,str+i+2); strcpy(mag2[count].mo,mag1[count].mo); strcpy(mag2[count].yu,mag1[count].yu); count++; } qsort(mag1,count,sizeof(Node),cmp1); qsort(mag2,count,sizeof(Node),cmp2); int n; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { gets(str); if(str[0]=='[') { f=search(str,count,0); if(f>=0) printf("%s/n",mag1[f].yu); else printf("what?/n"); } else { f=search(str,count,1); if(f>=0) { strcpy(str, mag2[f].mo+1); str[strlen(str)-1] = '/0'; printf("%s/n",str); } else printf("what?/n"); } } return 0; }
相关文章推荐
- HDU-1880-魔咒词典【哈希(Hash)+二分】
- HDU 1880 魔咒词典
- hdu1880之魔咒词典
- hdu1880 魔咒词典 (hash)
- hdu 1880 魔咒词典(字符串hash)
- 魔咒词典(hdu 1880)
- HDU 1880 魔咒词典
- hdu 1880 魔咒词典 (字符串哈希)
- HDU 1880 魔咒词典 字符串
- HDU 1880 魔咒词典 (二分搜索)
- Hdu 1880 魔咒词典(hash链表)
- hdu 1880 魔咒词典--字符串简单查找
- HDU1880-魔咒词典
- HDU 1880 魔咒词典
- hdu 1880 魔咒词典
- hdu1880魔咒词典 字符串哈希
- hdu 1880 魔咒词典
- HDU1880——哈希表(BKDR算法)——魔咒词典
- HDU 1880 魔咒词典
- hdu(1880):魔咒词典——字符串hash的应用