【留坑】 POJ2503 注意输入输出&&几种数据结构的复习、比较(线性表建立的字典 || 树建立的字典_即Trie || hash || map)
2016-11-26 21:46
567 查看
1 题意
2 分析
(上限,3000ms)
1)代码一,map,G++,1700ms~2500ms
①注意map不能将char数组作为关键字或者映射值,除非重载<,否则插不进去的。。
②gets(temp)会读入空白行,内部是通过temp[0]=='\0'来判断,是不是空白行,如果是就不会输出,我们同样可以用这一点,来区别两组用空白行分割的输入。
③因为map的未插入的key-值,其由于初始化而对应的值是null,如果值的类型是string,那么map_name[key]=="\0",就是说明没有插入,与find()类似作用,不过速度比find()稍慢。(.find()速度为1700ms+,另一个速度为2500ms+,原因不明...)
map<string,string> m;
m[temp][0]=='\0'或者m[temp]=="\0"或者m.find(temp)==m.end() 都代表temp没有插入该map。
④代码一 C++ CE,G++ AC。原因不明...
2)代码二 ,sort+bsearch,G++,600ms+
3)代码三,hash,G++,300ms+
4)代码四,Trie,复习完树再补...
3
代码一 map,1700ms~2500ms
代码二 线性表建立的字典+sort+二分,600ms? 怎么比别人慢呢
代码三 hash ,300ms+
代码四 Trie
2 分析
(上限,3000ms)
1)代码一,map,G++,1700ms~2500ms
①注意map不能将char数组作为关键字或者映射值,除非重载<,否则插不进去的。。
②gets(temp)会读入空白行,内部是通过temp[0]=='\0'来判断,是不是空白行,如果是就不会输出,我们同样可以用这一点,来区别两组用空白行分割的输入。
③因为map的未插入的key-值,其由于初始化而对应的值是null,如果值的类型是string,那么map_name[key]=="\0",就是说明没有插入,与find()类似作用,不过速度比find()稍慢。(.find()速度为1700ms+,另一个速度为2500ms+,原因不明...)
map<string,string> m;
m[temp][0]=='\0'或者m[temp]=="\0"或者m.find(temp)==m.end() 都代表temp没有插入该map。
④代码一 C++ CE,G++ AC。原因不明...
2)代码二 ,sort+bsearch,G++,600ms+
3)代码三,hash,G++,300ms+
4)代码四,Trie,复习完树再补...
3
代码一 map,1700ms~2500ms
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <map> using namespace std; //map <char*,char*> mmap; map不能用char* 作为关键字和映射值,毕竟指针,代表地址 map <string,string> mmap; char english[30],foreign[30],temp[60]; int main() { while(gets(temp)&&temp[0]!='\0'){ sscanf(temp,"%s %s",&english,&foreign); mmap[foreign]=english; } while(gets(temp)&&temp[0]!='\0'){ /* //第一种查找姿势: if(mmap[temp][0]=='\0') /// OR mmap[temp]="\0" cout<<"eh"<<endl; else cout<<mmap[temp]<<endl; //printf("%s\n",mmap[temp]); string 不能用printf输出 */ //第二种查找姿势: if(mmap.find(temp)!=mmap.end()) cout<<mmap[temp]<<endl; else cout<<"eh"<<endl; } }
代码二 线性表建立的字典+sort+二分,600ms? 怎么比别人慢呢
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <map> using namespace std; const int maxn=100100; struct Zidian{ char english[30]; char foreign[30]; }zidian[maxn]; char temp[60]; bool cmp(struct Zidian z1,struct Zidian z2){ return strcmp(z1.foreign,z2.foreign)<0?true:false; } int Bsearch(int l,int r){ while(l<=r){///l<=r: l=1,r=5,find=1; it needs '='. int m=(l+r)>>1; int judge=strcmp(zidian[m].foreign,temp); if(judge==0) return m; else if(judge<0) l=m+1; else if(judge>0) r=m-1; //Bsearch(l,r); 小伙,想嘛呢,我说超时了 } return -1; } int main() { int cur=0; while(gets(temp)&&temp[0]!='\0'){ sscanf(temp,"%s %s",&zidian[cur].english,&zidian[cur].foreign); cur++; } sort(zidian,zidian+cur,cmp); while(gets(temp)&&temp[0]!='\0'){ int pos=Bsearch(0,cur-1); if(pos==-1) cout<<"eh"<<endl; else cout<<zidian[pos].english<<endl; } }
代码三 hash ,300ms+
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <map> using namespace std; const int maxn=120007; const int mod=maxn; struct Node{ int next; char english[30]; char foreign[30]; }hash_[maxn]; int cur=1; int hash_head[maxn]; char temp[60],english[30],foreign[30]; void Hash(){ int key=0,len=strlen(foreign); for(int i=0;i<len;i++){ key=(key*27+foreign[i]-'0')%mod; } strcpy(hash_[cur].english,english); strcpy(hash_[cur].foreign,foreign); hash_[cur].next=hash_head[key]; hash_head[key]=cur++; } int Find() { int key=0,len=strlen(temp); for(int i=0;i<len;i++) key=(key*27+temp[i]-'0')%mod; int u=hash_head[key]; while(u>0){ if(strcmp(hash_[u].foreign,temp)==0) return u; u=hash_[u].next; } return -1; } int main(){ while(gets(temp)&&temp[0]!='\0'){ sscanf(temp,"%s %s",&english,&foreign); Hash(); } while(gets(temp)&&temp[0]!='\0'){ int index=Find(); if(index==-1) cout<<"eh"<<endl; else{ cout<<hash_[index].english<<endl; } } return 0; }
代码四 Trie
相关文章推荐
- ACM: hash题 poj 2503
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- POJ 2503 Babelfish(人造字典map)
- POJ_2503_Babelfish_(Trie/map)
- Poj 2503 Babelfish (Trie / STL / BKDR Hash)
- poj 2503 Babelfish (map,trie 树)
- POJ 2503 Babelfish [map & string]
- 『Python CoolBook』数据结构和算法_字典比较&字典和集合
- poj_2503_Babelfish(字典树&&map)
- POJ 2503 Babelfish(hash + map)
- 哈希表Hashtable与字典表Dictionary<K,V>的比较。(多线程推荐hash,单线程推荐dictionary)
- 例说数据结构&STL(十一)——hash_map/unordered_map
- poj 2503 【hash】【map】
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- HDU-1496-Equations && POJ-1840-Eqs (hash)
- mybatis if 动态生成SQL的一个注意点 parameterType="map"
- POJ_2503(基本Hash)
- poj 2945 Find the Clones (map+string,hash思维)
- Linux下map、hash_map和unordered_map效率比较
- POJ 3576 Hash Trie