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.不过感觉对字典树有了 ...
随机推荐
- Device Tree(一):背景介绍
一.前言 作为一个多年耕耘在linux 2.6.23内核的开发者,各个不同项目中各种不同周边外设驱动的开发以及各种琐碎的.扯皮的俗务占据了大部分的时间.当有机会下载3.14的内核并准备学习的时候,突然 ...
- BZOJ 2566 xmastree(树分治+multiset)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...
- 解决 Eclipse 重写方法时参数为arg0,arg1
我们在用eclipse 中Alt+ / 补全代码时.方法重写会出现arg0这样可读性差的参数. 原因是没有找到android 的源文件. 一.下载文件 需要下载 sources for android ...
- poj 1556 (Dijkstra + Geometry 线段相交)
链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- 面向对象的JavaScript系列二,继承
1.原型链 function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- MongoDB资料汇总
MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 它的特点是高性能.易部署.易使用,存储数据非常方便.主要功能特性有: 面向集合存 ...
- idea编辑器HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath();方法无法使用
HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath(); 前几天在使用idea的时候发现这个方 ...
- (转)使用Jmeter进行http接口测试
前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...
- 微信开发时遇到的UrlConnection乱码的问题
昨天做一个微信的模板消息推送的功能,功能倒是很快写完了,我本地测试微信收到的推送消息是正常的,但是一部署到服务器后微信收到的推送消息就变成乱码了. 为了找到原因,做了很多测试,查了一下午百度,最后得出 ...