fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Main
  5. {
  6. static ArrayList<ArrayList<String>> listM = new ArrayList<ArrayList<String>>(53);
  7. static int listsize;
  8.  
  9. public static void main (String [] args) throws IOException
  10. {
  11. while(true)
  12. {
  13. String str1,str2;
  14. str1=br.readLine();if(str1==null)break;
  15. if(str1.charAt(0)=='#')break;
  16. str2=br.readLine();str1+=" "+str2;
  17. String [] temp = str1.split(" ");
  18.  
  19. for(String t : temp)
  20. {
  21. ArrayList<String> list = new ArrayList<String>();
  22. list.add(t);listM.add(list);
  23. }
  24. ArrayList<String> list = new ArrayList<String>();
  25. list.add("zz");for(int i=0;i<3;i++)listM.add(0,list);
  26. listsize=listM.size();
  27. for(int i=4;i<listsize;)
  28. {
  29. int trans=check(i);
  30. if(trans!=0)i=i-trans-1;i++;
  31. }
  32. System.out.print((listsize-3)+" piles remaining:");
  33. for(int i=3;i<listsize;i++)
  34. {
  35. System.out.print(" "+listM.get(i).size());
  36. }
  37. System.out.println();listM.clear();
  38. }
  39.  
  40.  
  41.  
  42. }
  43.  
  44. public static int check(int index)
  45. {
  46. ArrayList<String> list1 = new ArrayList<String>();ArrayList<String> list2 = new ArrayList<String>();ArrayList<String> list3 = new ArrayList<String>();
  47. list1=listM.get(index);
  48. String str1=list1.get(list1.size()-1);
  49. list2=listM.get(index-1);
  50. String str2=list2.get(list2.size()-1);
  51. list3=listM.get(index-3);
  52. String str3=list3.get(list3.size()-1);
  53. if(str1.charAt(0)==str3.charAt(0) || str1.charAt(1)==str3.charAt(1))
  54. {
  55. list3.add(str1);list1.remove(list1.size()-1);
  56. listM.set(index-3,list3);
  57. if(list1.size()==0){listM.remove(index);listsize--;}
  58. else listM.set(index,list1);
  59. return 3;
  60. }
  61. if(str1.charAt(0)==str2.charAt(0) || str1.charAt(1)==str2.charAt(1))
  62. {
  63. list2.add(str1);list1.remove(list1.size()-1);
  64. listM.set(index-1,list2);if(list1.size()==0){listM.remove(index);listsize--;}
  65. else listM.set(index,list1);
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. }
Success #stdin #stdout 0.14s 60128KB
stdin
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
#
stdout
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52
6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52