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

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

二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到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. Eclipse中没有andriod问题解决方法

    按照网上教程<andriod+环境搭建_图文版>进行安装android,结果,在eclipse中window->preference下找不到"Android"选项 ...

  2. VS2013自动注释插件

    在程序编写的时候,你是否见过这种写法?整个项目每个cs文件头部都包含一个,版权,版本等信息的注释头? 类似这个类文件: /*************************************** ...

  3. linux 查找替换

    esc:   命令模式与插入模式的切换 一.vi查找:    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢?在vi里可没有菜单-〉查找, 不过没关系,你在命令模式下敲斜杆( ...

  4. linux sudo apt-get用法详解

    APT的使用(Ubuntu Linux软件包管理工具一)apt-cache search # ------(package 搜索包)apt-cache show #------(package 获取包 ...

  5. [CVE:2013-4810]Apache Tomcat/JBoss远程命令执行

    <?php $host=gethostbyname($argv[1]); $port=$argv[2]; $cmd=$argv[3]; //small jsp shell //change th ...

  6. C运行时的数据结构

    本文描述一下:C运行时的数据结构,相关的段,压栈等 unix默认的编译器会将编译生成的文件默认命名为a.out 目标文件和可执行文件可以有几种不同的格式,所有这些不同格式具有一个共同的概念,那就是段. ...

  7. server-pc--------------->lspci,lsusb,meminfo等配置信息

    安装yum install pciutils usbutils [root@server09 ~]# [root@server09 ~]# lspci00:00.0 Host bridge: Inte ...

  8. chrome离线安装包_下载

    ZC: 如何下载的文章 参考:“https://www.tekrevue.com/tip/download-chrome-offline-installer/” 1. f/q 2.下载网址: 2.1. ...

  9. js 函数-Tom

    函数类型 在ECMAScript 中有三种函数类型:函数声明,函数表达式和函数构造器创建的函数.每一种都有自己的特点. 函数声明 函数声明(缩写为FD)是这样一种函数: 有一个特定的名称 在源码中的位 ...

  10. Docker-创建支持ssh服务的镜像

    这里测试tomcat镜像安装ssh服务 1.启动镜像 [root@wls12c docker]$ docker run -d tomcat:centos 844bdde121a03174f3abd22 ...