hdu1880
来源:互联网 发布:淘宝用户信息查询 编辑:程序博客网 时间:2024/05/21 09:23
这道题目卡内存卡得厉害,多次尝试得出结论用string一定会MLE,因为string对象一般都会预留空间;使用二恶心分查找 hash map都可以实现,二分查找更大众化,第二次使用的时候又碰到一个更恶心的事情 搞了我一个小时才发现 郁闷 知道这道题目卡内存 故意在声明结构体时比较精打细算 魔法名设为21 后来才发现最大20个字母是不包括两个括号字符的 有点不懂 怎么不会报运行时错误,越界或者空间不够,(测试过是不会报错 这种问题只能在编译时提醒 运行过程中尽最大努力利用空间,字符串越界会出现一些不可预期的行为 没有终止空格了)
看来zoj试题的测试用例一般都包含边界情况,一定要小心 有时候浪费一点空间还是保险一点
#include <iostream>
#include <algorithm>
using namespace std;
struct code
{
char name[25];
char func[85];
};
code record1[100000];
code record2[100000];
void bi_search(char* s,int low,int high,int tag)
{
int mid;
if(tag==0)
{
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(record1[mid].name,s)==0){cout<<record1[mid].func<<endl;return;}
else if(strcmp(record1[mid].name,s)>0)high=mid-1;
else low=mid+1;
}
cout<<"what?"<<endl;
}
else
{
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(record2[mid].func,s)==0)
{
int len=strlen(record2[mid].name);
record2[mid].name[len-1]='/0';
cout<<record2[mid].name+1<<endl;
record2[mid].name[len-1]=']';
return;
}
else if(strcmp(record2[mid].func,s)>0)high=mid-1;
else low=mid+1;
}
cout<<"what?"<<endl;
}
}
int cmp1(code x,code y)
{
return strcmp(x.name,y.name)<0;
}
int cmp2(code x,code y)
{
return strcmp(x.func,y.func)<0;
}
int main()
{
int top=-1;
char input[120];
while(gets(input)&&strcmp(input,"@END@")!=0)
{
int i=0;
while(input[i]!=']')i++;
//++top;
strncpy(record1[++top].name,input,i+1),record1[top].name[i+1]='/0';
strcpy(record2[top].name,record1[top].name);
int len=strlen(input);
strncpy(record1[top].func,input+i+2,len-i-2),record1[top].func[len-i-2]='/0';
strcpy(record2[top].func,record1[top].func);
}
//for(int j=0;j<=top;j++)
//cout<<record1[j].name<<record1[j].func<<endl;
sort(record1,record1+top+1,cmp1);
sort(record2,record2+top+1,cmp2);
//for( j=0;j<=top;j++)
//cout<<record1[j].name<<record1[j].func<<endl;
int n;
cin>>n;
getchar();//qu diao huan hang
while(n--)
{
gets(input);
if(input[0]=='[')bi_search(input,0,top,0);
else bi_search(input,0,top,1);
}
return 0;
}
- hdu1880
- hdu1880
- hdu1880
- HDU1880 字符串hash
- HDU1880-魔咒词典
- hdu1880 hash算法学习
- hdu1880(魔咒词典)
- HDU1880 简单字符串哈希
- hdu1880之魔咒词典
- hdu1880 魔咒词典 (hash)
- hdu1880魔咒词典 字符串哈希
- 何去何从
- 牛人聊天记录
- ubuntu 9.10 更换IP地址 解决不能上网问题
- MVC
- ubuntu apt-get常用命令
- hdu1880
- 数据库初级
- 新手
- 怎么使用javascript实现类的功能
- 内核-编译器-busybox版本之间的关系
- 从项目中得到的关系型数据库的一个设计原则
- Ubuntu9.10-GNUStep环境下的Objective-C多文件组织
- 数据库中级
- C++ 调C C调c++