您的位置:首页 > 理论基础 > 数据结构算法

【留坑】 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

#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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: