BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #34111同步于 2009/12/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

大牛帮我优化一下我的程序。

lichehuo
2009/12/23镜像同步3 回复
这是ACM上的1003题。我提交之后是运行时错误。 我的思路是,输入字符串之后,冒泡排序,再折半查找。 #include <iostream> #include <string> using namespace std; #define MAXN 500 #define MAXM 100 void mysort(string *p,const int length); void swap(string&,string&); void myfind(const string *,const string,int); int main() { int sum,count; string str[MAXN]; string str_find[MAXM]; string *my=str; cin>>sum; int length=sum; while(sum--) { cin>>str[sum]; } mysort(str,length); cin>>count; int a=count; while(count--) { cin>>str_find[count]; } for(int i=a-1;i>=0;--i) { myfind(str,str_find[i],length); } return 0; } void mysort(string *p,const int length) { for(int i=length-1;i>=0;--i) for(int j=0;j<i;++j) { if(*(p+i)<*(p+j)) swap(*(p+i),*(p+j)); } } void swap(string &a,string &b) { string temp=a; a=b; b=temp; } void myfind(const string *str,const string str_find,int length) { int low=0,high=length-1,mid; while(low<=high) { mid=low+((high-low)/2); if(*(str+mid)==str_find) { cout<<"YES"<<endl; return; } if(*(str+mid)>str_find) high=mid-1; else low=mid+1; } cout<<"NO"<<endl; }
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
Raiden机器人#1 · 2009/12/23
不是大牛 快排+二分吧……冒泡n^2要超时的……
lichehuo机器人#2 · 2009/12/23
【 在 Raiden 的大作中提到: 】 : 不是大牛 : 快排+二分吧……冒泡n^2要超时的…… 那我试试快排吧。
wolf5x机器人#3 · 2009/12/24
#define MAXN 500 #define MAXM 100 how do you know MAXN is 500, not 500000 ?