【一题多解】 map 二分 hash trie poj 2503
各种方式解这道题!!
利用map 超时了
#include <iostream> #include <string> #include <map> using namespace std; string s; map<string,int> in; int main() { in.clear(); int i=0,num=0; char out[100010][12]; char a[12],b[12],c[12]; char ch=getchar(); while(ch!='\n') { int j=0; while(ch>='a' && ch<='z') { a[j++]=ch; ch=getchar(); } a[j]='\0'; while(ch>'z' || ch<'a') ch=getchar(); j=0; while(ch>='a' && ch<='z') { b[j++]=ch; ch=getchar(); } b[j]='\0';//每个数组的结束符 num++; s=b; in[s]=num; strcpy(out[num],a); //while(ch!='\n') ch=getchar(); ch=getchar(); i++; } while(scanf("%s",c)!=EOF) { s=c; i=in[s]; if(i>0) printf("%s\n",out[i]); else printf("eh\n"); } system("pause"); return 0; }
二分查找
#include<iostream> using namespace std; struct pp { char a[12],b[12]; }p[100010]; int cmp(void const *c,void const *d) { return strcmp(((pp *)c)->b,((pp *)d)->b); } int binarysearch(char s[],int k) { int low=0,hight=k+1,mid; while(low<=hight) { mid=(low+hight)/2; if(strcmp(p[mid].b,s)==0) return mid; else if(strcmp(p[mid].b,s)<0) low=mid+1; else hight=mid-1; } return -1; } int main() { char ch,s[12]; int i,k,j,t; ch=getchar();i=0;p[0].a[0]=ch;k=-1; while(1) { k++; while(ch!=' ') { p[k].a[i++]=ch; ch=getchar(); } p[k].a[++i]='\0';i=0; ch=getchar();j=0; while(ch!='\n') { p[k].b[j++]=ch;ch=getchar(); } p[k].b[j]='\0';j=0; ch=getchar(); if(ch=='\n')break; } qsort(p,k+1,sizeof(p[0]),cmp); while(scanf("%s",s)!=EOF) { t=binarysearch(s,k); if(t>=0) printf("%s\n",p[t].a); else printf("eh\n"); } return 0; }
hash表
#include<cstdio> #include<iostream> #include<string> using namespace std; const int M=20627;//Hash Table的槽数,取质数对解决冲突较为有效 char English[100000][11],Foreign[100000][11]; struct node//Hash Table中的元素类型 { int num;//记录英语数组中的标号 struct node *next; }*link[M]={NULL};//槽中存放一个链表头指针(开散列解决冲突) int ELFlash(char *key)// UNIX 系统ELF字符串散列函数,对字符串的处理一般使用此函数 { unsigned long h=0; while (*key) { h=(h<<4)+*key++; unsigned long g=h & 0xf0000000L; if (g) h^=g>>24; h &= ~g; } return h%M; } int main() { char st[30]; int i,k; node *p; i=0; while (gets(st)) { if (sscanf(st,"%s %s",English[i],Foreign[i])!=2) break;//判断读入词典是否完毕 k=ELFlash(Foreign[i]);//计算Hash函数 p=new node;//建立Hash Table p->num=i; p->next=link[k]; link[k]=p; i++; } while (gets(st)!=NULL) { k=ELFlash(st); p=link[k]; while (p!=NULL)//在Hash Table中查找单词 { if (strcmp(st,Foreign[p->num])==0) break;//若找到则推出 p=p->next; } if (p==NULL) printf("eh\n");//没有找到的情况 else printf("%s\n",English[p->num]); } return 0; }
trie
#include<iostream> using namespace std; struct node{ node *child[26]; char key[20]; bool d; }*root; char s1[20],s2[20],s3[20]; void trie(){ root=new node; for(int i=0;i<26;i++) root->child[i]=NULL; root->d=false; } void insert(){ node *r=root,*p; int len=strlen(s3); for(int i=0;i<len;i++){ if(r->child[s3[i]-'a']==NULL){ p=new node; p->d=false; for(int j=0;j<26;j++) p->child[j]=NULL; r->child[s3[i]-'a']=p; } r=r->child[s3[i]-'a']; if(i==len-1){ r->d=true; strcpy(r->key,s2); } } } bool search(){ node *r=root; int len=strlen(s1); for(int i=0;i<len;i++){ r=r->child[s1[i]-'a']; if(r==NULL) return false; if(i==len-1 && r->d){ strcpy(s2,r->key); return true; } } return false; } int main() { int i,j,k,len; trie(); while(true){ gets(s1); if(strcmp(s1,"")==0) break; for(i=0;s1[i]!=' ';i++) s2[i]=s1[i]; for(i=i+1, j=0, len=strlen(s1);i<len;i++,j++) s3[j]=s1[i]; insert(); } while(scanf("%s",s1)!=EOF){ if(search()) printf("%s\n",s2); else printf("eh\n"); } system("pause"); return 0; }
再来个通过的map..
#include <stdio.h> #include <map> #include <string> #include <iostream> using namespace std; int main() { // freopen("in.txt","r",stdin); string s; char ss[40],s1[20],s2[20]; map <string,string> m; map <string,string> ::iterator p; while (gets(ss)) //建立map { s=ss; if (s[0]<'a' || s[0]>'z' ) break; else { sscanf(s.c_str(),"%s %s",s1,s2); m[s2]=s1; } } while (gets(ss)) //从map查询 { s=ss; if (s[0]<'a' || s[0]>'z' ) break; p=m.find(s); if ( p!=m.end()) cout << p->second <<endl; else printf("eh\n"); } return 0; }