【九度OJ】1029【二分查找】
2014-08-10 17:38
495 查看
写完二分查找,变成600MS了。。。300MS的到底怎么写的。。。
代码:
代码:
package Test1; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Test10_2_1029 { /** * by qr jobdu 1029 2014-8-10 */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); List<String> incantation1=new ArrayList<String>(); //咒语在前,功能在后 List<String> incantation2=new ArrayList<String>(); //功能在前,咒语在后 String str = scan.nextLine(); while (!str.equals("@END@")) { incantation1.add(str); str=str.substring(str.indexOf("]") + 2)+" "+str.substring(0, str.indexOf("]") + 1); incantation2.add(str); str = scan.nextLine(); } Collections.sort(incantation1); Collections.sort(incantation2); int n =Integer.parseInt(scan.nextLine()); for (int i = 0; i < n; i++) { str = scan.nextLine(); //binary search int low=0; int high=incantation1.size()-1; int mid=0; if(str.startsWith("[")){ //给出咒语查找对应功能 使用incantation1 while(low<=high){ mid=(low+high)/2; String s=incantation1.get(mid); s=s.substring(0,s.indexOf("]")+1); if(s.compareTo(str)>0){ high=mid-1; }else if(s.compareTo(str)<0){ low=mid+1; }else{ str=incantation1.get(mid); System.out.println(str.substring(str.indexOf("]")+2)); break; } } }else{ //使用incantation2 while(low<=high){ mid=(low+high)/2; String s=incantation2.get(mid); s=s.substring(0,s.indexOf("[")-1); if(s.compareTo(str)>0){ high=mid-1; }else if(s.compareTo(str)<0){ low=mid+1; }else{ str=incantation2.get(mid); System.out.println(str.substring(str.indexOf("[")+1,str.indexOf("]"))); break; } } } if(low>high) System.out.println("what?"); } } }
相关文章推荐
- 理解二分查找!!! 九度OJ-1534,数组中第K小的数字。
- 九度OJ 1069 查找学生信息(二分查找)
- 【九度OJ】1029【HashMap查找】
- 【九度OJ】1029【HashMap查找】【版本二】
- 九度OJ 1533 最长上升子序列 (基于贪心和二分查找)
- 九度:1029<魔咒词典><排序查找>
- 九度OJ月赛---题目1534:数组中第K小的数字 (二分搜索)
- 最长上升子序列, N*logN,九度OJ 1533,二分+DP
- 题目1173:查找 九度OJ
- 九度OJ+贪心二分+最大值最小化都是这样做的。
- OJ递归之二分查找法
- 九度OJ 1349:数字在排序数组中出现的次数 (排序、查找)
- 【九度OJ】题目1177:查找 解题报告
- 九度oj 题目1177:查找--string的替换、删除、反转
- 题目1176:树查找 九度OJ
- 九度OJ 1177 查找 (模拟)
- 九度OJ 1081 递推数列 -- 矩阵二分乘法
- 【九度OJ】题目1173:查找 解题报告
- <九度 OJ>题目1069:查找学生信息
- 九度OJ 1081 递推数列 -- 矩阵二分乘法