fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Main{
  5. public static void main(String args[]) throws IOException{
  6.  
  7. int n = Integer.parseInt(input.readLine());
  8.  
  9. LinkedList<Integer> target = new LinkedList<Integer>();
  10. LinkedList<Integer> condition = new LinkedList<Integer>();
  11.  
  12. StringTokenizer st1 = new StringTokenizer(input.readLine());
  13. StringTokenizer st2 = new StringTokenizer(input.readLine());
  14. for (int i = 0; i < n; i++){
  15. target.offerLast(Integer.parseInt(st1.nextToken()));
  16. condition.offerLast(Integer.parseInt(st2.nextToken()));
  17. }
  18.  
  19. while (condition.peekFirst() != target.peekFirst()){
  20. condition.offerLast(condition.pollFirst());
  21. }
  22.  
  23. boolean isGoodPuzzle = true;
  24.  
  25. ArrayList<Integer> targetarr = convertList(target);
  26. ArrayList<Integer> conditionarr = convertList(condition);
  27.  
  28. for (int i = 1; i < n; i++){
  29. if (targetarr.get(i) != conditionarr.get(i)){
  30. isGoodPuzzle = false;
  31. break;
  32. }
  33. }
  34.  
  35. if (!isGoodPuzzle){
  36. isGoodPuzzle = true;
  37.  
  38. int temp = condition.pollFirst();
  39. condition = reverseList(condition);
  40. condition.addFirst(temp);
  41.  
  42. conditionarr = convertList(condition);
  43.  
  44. for (int i = 1; i < n; i++){
  45. if (targetarr.get(i) != conditionarr.get(i)){
  46. isGoodPuzzle = false;
  47. break;
  48. }
  49. }
  50. }
  51.  
  52. System.out.println(isGoodPuzzle ? "good puzzle" : "bad puzzle");
  53. }
  54.  
  55. public static LinkedList<Integer> reverseList (LinkedList<Integer> arr){
  56. LinkedList<Integer> target = new LinkedList<Integer>();
  57.  
  58. while(!arr.isEmpty()){
  59. target.offerLast(arr.pollLast());
  60. }
  61.  
  62. return target;
  63. }
  64.  
  65. public static ArrayList<Integer> convertList(LinkedList<Integer> arr){
  66. ArrayList<Integer> target = new ArrayList<Integer>();
  67.  
  68. target.add(arr.pollFirst());
  69.  
  70. return target;
  71. }
  72. }
Runtime error #stdin #stdout #stderr 0.1s 27892KB
stdin
5
1 2 3 4 5
4 3 2 1 5
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at Main.main(Main.java:30)