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

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

二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到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. 关于async & await(TAP)异步模型的异常捕获

    在TAP之前,若要捕获线程中Task的异常,通常有两种办法: 1.阻塞:线程开始后,在适当的时机,调用 wait,或waitAll方法. 2.非阻塞(推荐):在建立任务的时候,写该task的conti ...

  2. duilib -- Label控件的bug(转载)

    转载:http://blog.csdn.net/rundll64/article/details/24823809?locationNum=6&fps=1 发现LabelUI的[属性列表.XM ...

  3. GetLastInputInfo计时用户离开电脑及软件在指定时间锁定等(转)

    /************************************************************************/ /* 说明: 调用函数GetLastInputIn ...

  4. 借助Nodejs在服务端使用jQuery采集17173游戏排行信息

    Nodejs相关依赖模块介绍 Nodejs的优势这里就不做介绍啦,这年头相信大家对它也不陌生了.这里主要介绍一下用到的第三方模块. async:js代码中到处都是异步回调,很多时候我们需要做同步处理, ...

  5. Vi编辑器下使用分屏显示【已自己验证所有】

    :new 水平分割出一个新窗口 :vnew,:vne 垂直分割出一个新窗口 :new+文件路径/文件名; 在新的水平分屏中 载入/新建 文件.[文件存在则载入,不存在则在指定的路径新建,下同] :vn ...

  6. Create,Insert

    创建表 create table people ( id int ,name ) ) create table toys ( id int ,name ) ,people_id int ) CREAT ...

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

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

  8. Servlet与Tomcat

    Web应用不仅局限于展示在服务器上的静态页面,更多的是根据用的请求动态的生成页面信息,还可以从数据库中提取数据,生成页面返回给用户. 第一种方法:遵循HTTP协议实现一个服务器端软件 第二种方法:利用 ...

  9. FJNU 1159 Fat Brother’s new way(胖哥的新姿势)

    FJNU 1159 Fat Brother’s new way(胖哥的新姿势) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题目 ...

  10. php 判断 xml 里是否存在某个节点

    参考网址:http://blog.csdn.net/crazyboy2005/article/details/6114454 DOMDocument中,怎样判断某节点是否存在呢? /* $xml-&g ...