hdu 1075 二分搜索
还是写一下,二分搜索好了
这道题开数组比较坑...
二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环
先上AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; struct Ch{
char a[12];
char b[12];
}; int cmp(const void *aa,const void *bb){
Ch *x = (Ch *) aa;
Ch *y = (Ch *) bb;
return strcmp(x->b,y->b)>0?1:-1;
} struct Ch ch[500000];
char temp[500000];
int sum=0; int main(){
int i=0,j;
scanf("%s",temp);
while(scanf("%s",temp),strcmp(temp,"END")!=0){
strcpy(ch[i].a,temp);
scanf("%s",ch[i++].b);
sum++;
}
qsort(ch,sum,sizeof(ch[0]),cmp);
scanf("%s",temp);
getchar();
while(gets(temp)!=NULL,strcmp(temp,"END")!=0){
char t[15]={0};
j=0; bool is=0;
for(i=0;i<strlen(temp);i++){
if((temp[i]>='a'&&temp[i]<='z')||(temp[i]>='A'&&temp[i]<='Z')){
t[j++]=temp[i];
is=1;
if(i!=strlen(temp)-1)
continue;
}
if(is){
is=0;
j=0;
int l=0,h=sum-1,mid;
while(l<=h){
mid=(l+h)/2;
if(strcmp(t,ch[mid].b)>0)
l=mid+1;
else if(strcmp(t,ch[mid].b)<0)
h=mid-1;
else if(strcmp(t,ch[mid].b)==0){
printf("%s",ch[mid].a);
break;
}
}
if(l>h)
printf("%s",t);
memset(t,0,sizeof(t));
}
if(temp[i]<'A'||(temp[i]>'Z'&&temp[i]<'a')||temp[i]>'z')
printf("%c",temp[i]);
}
printf("\n");
}
return 0;
}
二分法的两种正确代码
1.左闭右闭
int search(int array[], int n, int v)
{
int mid, l = 0, h = n - 1; while (l <= h){
mid = (l + h) / 2;
if (array[mid] > v){
h = mid - 1;
}
else if (array[mid] < v){
l = mid + 1;
}
else{
return mid;
}
}
return -1;
}
2.左闭右开
int search(int array[], int n, int v)
{
int mid, l = 0, h = n; while (l < h){
mid = (l + h) / 2;
if (array[mid] > v){
h = mid;
}
else if (array[mid] < v){
l = mid + 1;
}
else{
return mid;
}
}
return -1;
}
hdu 1075 二分搜索的更多相关文章
- HDU 1075 What Are You Talking About(Trie的应用)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075 (map)
http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...
- hdu 1075 What Are You Talking About
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:比较简单,易懂,这里不做说明. 解法:第一种方法:用map映射,耗时1000+ms:第二种 ...
- 字典树 HDU 1075 What Are You Talking About
http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}
- 题解报告:hdu 1075 What Are You Talking About
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...
- HDU 1075 What Are You Talking About (Trie)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)
Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the lan ...
- hdu 1075 What Are You Talking About(字典树)
刚学的字典树,代码写得很不熟练.写法上也没有什么特别的优化,就是以1A为第一目标! 可惜还是失败了. 少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A.不过感觉对字典树有了 ...
随机推荐
- Python命令行解析argparse常用语法使用简介
查看原文:http://www.sijitao.net/2000.html python中的命令行解析最简单最原始的方法是使用sys.argv来实现,更高级的可以使用argparse这个模块.argp ...
- 终止jQuery的$.ajax方法abort
最近遇到,如果用户频繁点击ajax请求,有两个问题: 1,如果连续点击了5个ajax请求,前4个其实是无效的,趁早结束节省资源. 2,更严重的问题是:最后一个发送的请求,响应未必是最后一个,有可能造成 ...
- VI编辑器
- ServiceStack.OrmLite 6 学习笔记 查
查 根据id var result = db.SingleById<Poco>(1); 根据字段 var customer = db.Single<Customer>(new ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- 最大后验估计 -- Maximum-a-Posteriori (MAP) Estimation
最大后验估计是根据经验数据获得对难以观察的量的点估计.与最大似然估计类似,但是最大的不同时,最大后验估计的融入了要估计量的先验分布在其中.故最大后验估计可以看做规则化的最大似然估计.
- Spring字符集过滤器CharacterEncodingFilter
Spring中的字符集过滤器可以很方便的为我们解决项目中出现的中文乱码问题,而且使用方法也很简单,只需要在web.xml文件中配置一下该过滤器,设置两个重要的参数(encoding和forceEnco ...
- [转载] 深入理解Linux修改hostname
原文: http://www.cnblogs.com/kerrycode/p/3595724.html 当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问 ...
- (三)NAND flash和NOR flash的区别详解
我们使用的智能手机除了有一个可用的空间(如苹果8G.16G等),还有一个RAM容量,很多人都不是很清楚,为什么需要二个这样的芯片做存储呢,这就是我们下面要讲到的.这二种存储设备我们都统称为“FLASH ...
- (三)结构体指针、sizeof
(一)结构体指针定义 今天上班写了一段测试代码,结果在linux下编译出现段错误,刚开始一直找不到原因,后来找了度娘才搞懂了.我先贴出来第一次写的代码以及gcc编译器下报的错误: #include&l ...