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

如何才能使下面的这段程序更快呢 ?

Rodman
2010/2/26镜像同步5 回复
在OJ上面刷的一道题,总是超时,请大牛给些参考 ~~~更换哪些方法能使下面的程序更快,谢谢啊 题目描述 : 总体上说就是输入两个字符串,第一个字符串为solution“答案”,第二个字符串为guess“猜测” 1.如果第一个字符串的所有字符都在第二个字符串当中,则“胜利” 2.如果第二个字符串当中有7个字符与第一个字符串不同,则输出“失败” 3.其他情况则输出“平局” input: cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1 output : Round 1 You win. Round 2 You chickened out. Round 3 You lose. 下面是我的代码 谢谢大家~ package HangManJudge; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner (System.in); int round; String solution; String guess; String result; while ((round=in.nextInt())!=-1){ solution = in.nextLine(); solution = in.nextLine(); guess = in.nextLine(); result = compare(solution,guess); System.out.println("Round "+round); System.out.println(result); } in.close(); } public static String compare(String solution,String guess) { int index; int wrongTimes = 0; boolean dead = false; boolean win = true; boolean stop ; for (int i = 0; i < guess.length()&&!dead; i++) { index = -1; stop = false; for (int j =0;j < solution.length()&&!stop;j++){ if (guess.charAt(i)==solution.charAt(j)){ index = j; stop = true; } } if (index <0){ wrongTimes++ ; win = false ; } if (wrongTimes==7) dead = true; } if (win) return "You win."; else if (dead) return "You lose."; else return "You chickened out."; } }
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
greedisgood机器人#1 · 2010/2/26
【 在 Rodman 的大作中提到: 】 : 在OJ上面刷的一道题,总是超时,请大牛给些参考 ~~~更换哪些方法能使下面的程序更快,谢谢啊 : 题目描述 : 总体上说就是输入两个字符串,第一个字符串为solution“答案”,第二个字符串为guess“猜测” : 1.如果第一个字符串的所有字符都在第二个字符串当中,则“胜利” : ................... 描述有错吧。比如abc和abcd,第一个字符串的所有字符都在第二个字符串当中,按说该胜利,但你程序执行是You chickened out。 建议问问题时表贴子写清楚。
Aaron12机器人#2 · 2010/2/26
Hash 【 在 Rodman 的大作中提到: 】 : 在OJ上面刷的一道题,总是超时,请大牛给些参考 ~~~更换哪些方法能使下面的程序更快,谢谢啊 : 题目描述 : 总体上说就是输入两个字符串,第一个字符串为solution“答案”,第二个字符串为guess“猜测” : 1.如果第一个字符串的所有字符都在第二个字符串当中,则“胜利” : ...................
wks机器人#3 · 2010/2/26
Set s = new HashSet(guess.toCharArray()); s.removeAll(solution.toCharArray()); if(s.size()>=7) { ... } 这样会不会快一些?
Rodman机器人#4 · 2010/3/2
【 在 greedisgood 的大作中提到: 】 : 描述有错吧。比如abc和abcd,第一个字符串的所有字符都在第二个字符串当中,按说该胜利,但你程序执行是You chickened out。 : 建议问问题时表贴子写清楚。 对的 应该是胜利 是我的代码有问题 ~~~ 这道题的特例很多 有兴趣的同学可以编程实现一下,很有意思 UvaOJ的489题 ,给大家贴下题目吧 In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows: The contestant tries to solve to puzzle by guessing one letter at a time. Every time a guess is correct, all the characters in the word that match the guess will be ``turned over.'' For example, if your guess is ``o'' and the word is ``book'', then both ``o''s in the solution will be counted as ``solved.'' Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once. ______ | | | O | /|\ | | | / \ __|_ | |______ |_________| If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game. If the contestant does not guess enough letters to either win or lose, the contestant chickens out. Your task as the ``Hangman Judge'' is to determine, for each game, whether the contestant wins, loses, or fails to finish a game. Input Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of -1 would indicate the end of all games (and input). Output The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results: You win. You lose. You chickened out. Sample Input 1 cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1 Sample Output Round 1 You win. Round 2 You chickened out. Round 3 You lose.
Rodman机器人#5 · 2010/3/2
re Aaron12 & wks 用了hashSet解决了超时的问题