还是写一下,二分搜索好了

这道题开数组比较坑...

二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到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 二分搜索的更多相关文章

  1. 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 ...

  2. hdu 1075 (map)

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...

  3. hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:比较简单,易懂,这里不做说明. 解法:第一种方法:用map映射,耗时1000+ms:第二种 ...

  4. 字典树 HDU 1075 What Are You Talking About

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}

  5. 题解报告:hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...

  6. 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 ...

  7. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  8. 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 ...

  9. hdu 1075 What Are You Talking About(字典树)

    刚学的字典树,代码写得很不熟练.写法上也没有什么特别的优化,就是以1A为第一目标! 可惜还是失败了. 少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A.不过感觉对字典树有了 ...

随机推荐

  1. SqlSever基础 union 与 union all的区别,即 重复项是否全部显示

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法

    网络原因无法下载virtual device,status 为0表示服务器没有响应.FQ下载吧,有VPN的小伙伴推荐这种. 或者直接手动下载ova虚拟机文件,然后将虚拟机文件导入到virtualbox ...

  3. C语言文法定义与C程序的推导过程

    program à external_declaration | program external_declaration <程序> ->  <外部声明> |  < ...

  4. 解决使用jQuery采用append添加的元素事件无效的方法

    <html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...

  5. javascript学习-原生javascript的小特效(简单的运动效果)

    前些日子看了个视频所以就模仿它的技术来为大家做出几个简单的JS小特效 一:运动特效(主要是通过改变元素的left,right,height,width,opacity来达到运动的效果) 我们今天做一个 ...

  6. 07 SQL优化技术

    本章提要------------------------------------------------------调优技术及什么时候使用------------------------------- ...

  7. 花神的数论题(数位dp)

    规定sum[i] 为i里面含1的个数 ,求从1-N sum[i]的乘积. 数为64位内的,也就是sum[i]<=64的,这样可以dp求出1-N中含k个1的数有多少个,快速幂一下就可以了. 有个地 ...

  8. Android Glide+CircleImageView实现加载圆形图片列表

    需求:要在列表中实现圆形图片的显示,控件可能和加载库会存在冲突 先上代码,至于其中源码,以后有空再分析 MainActivity public class MainActivity extends A ...

  9. golang 资源

    1.Learning Go <学习Go语言> http://www.miek.nl/projects/learninggo/中文版http://mikespook.com/learning ...

  10. JavaScript设计模式-单例模式、模块模式(转载 学习中。。。。)

    (转载地址:http://technicolor.iteye.com/blog/1409656) 之前在<JavaScript小特性-面向对象>里面介绍过JavaScript面向对象的特性 ...