fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. //標準入力を配列に入れる
  13. Scanner sc = new Scanner(System.in);
  14. String b=sc.nextLine();
  15. int c=Integer.parseInt(b);
  16. String line[]=new String[c];
  17. for(int n=0;n<c;n++){
  18. line[n]=sc.nextLine();
  19. }
  20.  
  21. List<String> list=new ArrayList<>();
  22.  
  23. for(int n=0;n<c;n++){
  24. int x=list.indexOf(line[n]);
  25. //含まれていない場合
  26. if(x<0){
  27. list.add(line[n]);
  28. }
  29. //含まれている場合
  30. else if(x>0){
  31. list.remove(x);
  32. list.add(0,line[n]);
  33. }
  34. }
  35. System.out.println(list.get(0));
  36. System.out.println(list.get(1));
  37. System.out.println(list.get(2));
  38. }
  39. }
Success #stdin #stdout 0.11s 29412KB
stdin
5
とまと
もも
りんご
きゅうり
りんご
stdout
りんご
とまと
もも