fork(1) download
  1. //
  2. // ttp://www.itmedia.co.jp/enterprise/articles/1004/03/news002_2.html
  3. //
  4.  
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10.  
  11. #define FOR(I,S,E) for(int (I)=(S); (I)<(E); (I)++)
  12. #define REP(I,E) FOR((I),0,(E))
  13.  
  14.  
  15. using std::string;
  16.  
  17. typedef std::vector<string> vstr;
  18. typedef std::vector<vstr> vvstr;
  19. typedef std::set<vstr> svstr;
  20. typedef std::set<string> sstr;
  21.  
  22. svstr isComplete(vstr &pai);
  23. int getPaiNumber(string pai);
  24. int isSamePaiTwo(string pai1, string pai2);
  25. int isSamePaiThree(string pai1, string pai2, string pai3);
  26. int isSameSuitTwo(string pai1, string pai2);
  27. int isSameSuitThree(string pai1, string pai2, string pai3);
  28. int isSequenceTwo(string pai1, string pai2);
  29. int isSequenceThree(string pai1, string pai2, string pai3);
  30.  
  31. int main() {
  32.  
  33.  
  34. vstr pai(14);
  35.  
  36. string str;
  37.  
  38. std::cin >> str;
  39.  
  40. REP(i,13) {
  41. pai[i] = str.substr(i, 1);
  42. }
  43.  
  44. sstr ans;
  45.  
  46. FOR(i,1,10) {
  47. string p = std::to_string(i);
  48. pai[13] = p;
  49. svstr res = isComplete(pai);
  50. for (svstr::iterator it = res.begin(); it != res.end(); it++) {
  51. REP(j, 5) {
  52. size_t x = (*it)[j].find(p);
  53. if (x == string::npos) continue;
  54. vstr tv(5);
  55. REP (k, 5) {
  56. if (k != j)
  57. tv[k] = "(" + (*it)[k] + ")";
  58. else {
  59. string ts = (*it)[k];
  60. tv[k] = "[" + ts.erase(x, 1) + "]";
  61. }
  62. }
  63. std::sort(tv.begin(), tv.end());
  64. str.clear();
  65. REP (k, 5) str += tv[k];
  66. ans.insert(str);
  67. }
  68. }
  69. }
  70.  
  71. for (sstr::iterator it = ans.begin(); it != ans.end(); it++) {
  72. std::cout << (*it) << std::endl;
  73. }
  74.  
  75. return 0;
  76. }
  77.  
  78.  
  79.  
  80. svstr isComplete(vstr &pai) {
  81.  
  82. #define EQ1(x,a) ((x)==(a))
  83. #define EQ2(x,a,b) (EQ1(x,a)||EQ1(x,b))
  84. #define EQ3(x,a,b,c) (EQ2(x,a,b)||EQ1(x,c))
  85. #define EQ4(x,a,b,c,d) (EQ2(x,a,b)||EQ2(x,c,d))
  86. #define EQ5(x,a,b,c,d,e) (EQ3(x,a,b,c)||EQ2(a,d,e))
  87. #define EQ6(x,a,b,c,d,e,f) (EQ3(x,a,b,c)||EQ3(x,d,e,f))
  88. #define EQ7(x,a,b,c,d,e,f,g) (EQ4(x,a,b,c,d)||EQ3(x,e,f,g))
  89.  
  90. #define NG2(a,b) (!isSamePaiTwo(pai[a],pai[b])&&!isSequenceTwo(pai[a],pai[b]))
  91. #define NG3(a,b,c) (!isSamePaiThree(pai[a],pai[b],pai[c]) \
  92. &&!isSequenceThree(pai[a],pai[b],pai[c]))
  93.  
  94. svstr res;
  95.  
  96. REP(j1,14) {
  97. REP(j2,14) {
  98. if EQ1(j2,j1) continue;
  99. if NG2(j1,j2) continue;
  100. REP(j3,14) {
  101. if EQ2(j3,j2,j1) continue;
  102. if NG3(j1,j2,j3) continue;
  103. REP(j4,14) {
  104. if EQ3(j4,j3,j2,j1) continue;
  105. REP(j5,14) {
  106. if EQ4(j5,j4,j3,j2,j1) continue;
  107. if NG2(j4,j5) continue;
  108. REP(j6,14) {
  109. if EQ5(j6,j5,j4,j3,j2,j1) continue;
  110. if NG3(j4,j5,j6) continue;
  111. REP(j7,14) {
  112. if EQ6(j7,j6,j5,j4,j3,j2,j1) continue;
  113. REP(j8,14) {
  114. if EQ6(j8,j6,j5,j4,j3,j2,j1) continue;
  115. if EQ1(j8,j7) continue;
  116. if NG2(j7,j8) continue;
  117. REP(j9,14) {
  118. if EQ6(j9,j6,j5,j4,j3,j2,j1) continue;
  119. if EQ2(j9,j8,j7) continue;
  120. if NG3(j7,j8,j9) continue;
  121. REP(j10,14) {
  122. if EQ6(j10,j6,j5,j4,j3,j2,j1) continue;
  123. if EQ3(j10,j9,j8,j7) continue;
  124. REP(j11,14) {
  125. if EQ6(j11,j6,j5,j4,j3,j2,j1) continue;
  126. if EQ4(j11,j10,j9,j8,j7) continue;
  127. if NG2(j10,j11) continue;
  128. REP(j12,14) {
  129. if EQ6(j12,j6,j5,j4,j3,j2,j1) continue;
  130. if EQ5(j12,j11,j10,j9,j8,j7) continue;
  131. if NG3(j10,j11,j12) continue;
  132. REP(j13,14) {
  133. if EQ6(j13,j6,j5,j4,j3,j2,j1) continue;
  134. if EQ6(j13,j12,j11,j10,j9,j8,j7) continue;
  135. REP(j14,14) {
  136. if EQ6(j14,j6,j5,j4,j3,j2,j1) continue;
  137. if EQ7(j14,j13,j12,j11,j10,j9,j8,j7) continue;
  138. if (!isSamePaiTwo(pai[j13], pai[j14])) continue;
  139. vstr te(4);
  140. te[0] = pai[j1] + pai[j2] + pai[j3];
  141. te[1] = pai[j4] + pai[j5] + pai[j6];
  142. te[2] = pai[j7] + pai[j8] + pai[j9];
  143. te[3] = pai[j10] + pai[j11] + pai[j12];
  144. REP(i, 4) {
  145. std::sort(te[i].begin(), te[i].end());
  146. }
  147. std::sort(te.begin(), te.end());
  148. te.push_back(pai[j13] + pai[j14]);
  149. res.insert(te);
  150. } } } } }
  151. } } } } }
  152. } } } }
  153.  
  154. return res;
  155. }
  156.  
  157. int getPaiNumber(string pai) {
  158. return std::stoi(pai);
  159. }
  160. int isSamePaiTwo(string pai1, string pai2) {
  161. return pai1.compare(pai2) == 0;
  162. }
  163. int isSamePaiThree(string pai1, string pai2, string pai3) {
  164. return isSamePaiTwo(pai1, pai2) && isSamePaiTwo(pai2, pai3);
  165. }
  166. int isSameSuitTwo(string pai1, string pai2) {
  167. return true;
  168. }
  169. int isSameSuitThree(string pai1, string pai2, string pai3) {
  170. return isSameSuitTwo(pai1, pai2) && isSameSuitTwo(pai2, pai3);
  171. }
  172. int isSequenceTwo(string pai1, string pai2) {
  173. return isSameSuitTwo(pai1, pai2)
  174. && getPaiNumber(pai2) - getPaiNumber(pai1) == 1;
  175. }
  176. int isSequenceThree(string pai1, string pai2, string pai3) {
  177. return isSequenceTwo(pai1, pai2) && isSequenceTwo(pai2, pai3);
  178. }
  179.  
Success #stdin #stdout 1.7s 3444KB
stdin
1112345678999
stdout
(11)(123)(345)(678)[89]
(11)(123)(345)(678)[99]
(11)(123)(345)(789)[67]
(11)(123)(345)(789)[68]
(11)(123)(345)(789)[99]
(11)(123)(345)(999)[67]
(11)(123)(345)(999)[68]
(11)(123)(345)(999)[78]
(11)(123)(345)(999)[79]
(11)(123)(345)(999)[89]
(11)(123)(456)(678)[99]
(11)(123)(456)(789)[99]
(11)(123)(456)(999)[67]
(11)(123)(456)(999)[68]
(11)(123)(456)(999)[78]
(11)(123)(456)(999)[79]
(11)(123)(456)(999)[89]
(11)(123)(567)(789)[34]
(11)(123)(567)(789)[99]
(11)(123)(567)(999)[34]
(11)(123)(567)(999)[45]
(11)(123)(567)(999)[46]
(11)(123)(567)(999)[78]
(11)(123)(567)(999)[89]
(11)(123)(678)(999)[34]
(11)(123)(678)(999)[35]
(11)(123)(678)(999)[45]
(11)(123)(678)(999)[46]
(11)(123)(678)(999)[56]
(11)(123)(678)(999)[57]
(11)(123)(789)(999)[34]
(11)(123)(789)(999)[35]
(11)(123)(789)(999)[45]
(11)(123)(789)(999)[46]
(11)(123)(789)(999)[56]
(11)(234)(456)(789)[99]
(11)(234)(456)(999)[78]
(11)(234)(456)(999)[79]
(11)(234)(456)(999)[89]
(11)(234)(567)(789)[13]
(11)(234)(567)(789)[99]
(11)(234)(567)(999)[13]
(11)(234)(567)(999)[78]
(11)(234)(567)(999)[89]
(11)(234)(678)(999)[13]
(11)(234)(678)(999)[45]
(11)(234)(678)(999)[56]
(11)(234)(678)(999)[57]
(11)(234)(789)(999)[13]
(11)(234)(789)(999)[45]
(11)(234)(789)(999)[46]
(11)(234)(789)(999)[56]
(11)(345)(567)(999)[12]
(11)(345)(567)(999)[89]
(11)(345)(678)(999)[12]
(11)(345)(678)(999)[13]
(11)(345)(678)(999)[23]
(11)(345)(678)(999)[24]
(11)(345)(789)(999)[12]
(11)(345)(789)(999)[13]
(11)(345)(789)(999)[23]
(11)(345)(789)(999)[24]
(11)(345)(789)(999)[56]
(11)(456)(678)(999)[12]
(11)(456)(678)(999)[13]
(11)(456)(678)(999)[23]
(11)(456)(789)(999)[12]
(11)(456)(789)(999)[13]
(11)(456)(789)(999)[23]
(11)(456)(789)(999)[24]
(11)(456)(789)(999)[34]
(11)(456)(789)(999)[35]
(111)(123)(456)(678)[9]
(111)(123)(456)(789)[9]
(111)(123)(456)(99)[67]
(111)(123)(456)(99)[68]
(111)(123)(456)(99)[78]
(111)(123)(456)(99)[79]
(111)(123)(456)(99)[89]
(111)(123)(456)(999)[7]
(111)(123)(456)(999)[8]
(111)(123)(567)(789)[4]
(111)(123)(567)(789)[9]
(111)(123)(567)(99)[45]
(111)(123)(567)(99)[46]
(111)(123)(567)(99)[78]
(111)(123)(567)(99)[79]
(111)(123)(567)(99)[89]
(111)(123)(567)(999)[4]
(111)(123)(567)(999)[8]
(111)(123)(678)(99)[45]
(111)(123)(678)(99)[46]
(111)(123)(678)(99)[56]
(111)(123)(678)(99)[57]
(111)(123)(678)(99)[89]
(111)(123)(678)(999)[4]
(111)(123)(678)(999)[5]
(111)(123)(789)(99)[45]
(111)(123)(789)(99)[46]
(111)(123)(789)(99)[56]
(111)(123)(789)(99)[57]
(111)(123)(789)(99)[67]
(111)(123)(789)(99)[68]
(111)(123)(789)(999)[4]
(111)(123)(789)(999)[5]
(111)(123)(789)(999)[6]
(111)(234)(456)(789)[9]
(111)(234)(456)(99)[78]
(111)(234)(456)(99)[79]
(111)(234)(456)(99)[89]
(111)(234)(456)(999)[7]
(111)(234)(456)(999)[8]
(111)(234)(567)(789)[9]
(111)(234)(567)(99)[78]
(111)(234)(567)(99)[79]
(111)(234)(567)(99)[89]
(111)(234)(567)(999)[8]
(111)(234)(678)(99)[45]
(111)(234)(678)(99)[56]
(111)(234)(678)(99)[57]
(111)(234)(678)(99)[89]
(111)(234)(678)(999)[5]
(111)(234)(789)(99)[45]
(111)(234)(789)(99)[46]
(111)(234)(789)(99)[56]
(111)(234)(789)(99)[57]
(111)(234)(789)(99)[67]
(111)(234)(789)(99)[68]
(111)(234)(789)(999)[5]
(111)(234)(789)(999)[6]
(111)(345)(567)(99)[12]
(111)(345)(567)(99)[89]
(111)(345)(567)(999)[2]
(111)(345)(567)(999)[8]
(111)(345)(678)(99)[12]
(111)(345)(678)(99)[23]
(111)(345)(678)(99)[24]
(111)(345)(678)(99)[89]
(111)(345)(678)(999)[2]
(111)(345)(789)(99)[12]
(111)(345)(789)(99)[23]
(111)(345)(789)(99)[24]
(111)(345)(789)(99)[56]
(111)(345)(789)(99)[67]
(111)(345)(789)(99)[68]
(111)(345)(789)(999)[2]
(111)(345)(789)(999)[6]
(111)(456)(678)(99)[12]
(111)(456)(678)(99)[13]
(111)(456)(678)(99)[23]
(111)(456)(678)(999)[2]
(111)(456)(678)(999)[3]
(111)(456)(789)(99)[12]
(111)(456)(789)(99)[13]
(111)(456)(789)(99)[23]
(111)(456)(789)(99)[24]
(111)(456)(789)(99)[34]
(111)(456)(789)(99)[35]
(111)(456)(789)(999)[2]
(111)(456)(789)(999)[3]
(111)(567)(789)(99)[12]
(111)(567)(789)(99)[13]
(111)(567)(789)(99)[23]
(111)(567)(789)(99)[24]
(111)(567)(789)(99)[34]
(123)(345)(678)(99)[11]
(123)(345)(678)(99)[89]
(123)(345)(678)(999)[1]
(123)(345)(789)(99)[11]
(123)(345)(789)(99)[67]
(123)(345)(789)(99)[68]
(123)(345)(789)(999)[1]
(123)(345)(789)(999)[6]
(123)(456)(678)(99)[11]
(123)(456)(678)(999)[1]
(123)(456)(789)(99)[11]
(123)(456)(789)(999)[1]
(123)(567)(789)(99)[11]
(123)(567)(789)(99)[34]
(234)(456)(789)(99)[11]
(234)(456)(789)(999)[1]
(234)(567)(789)(99)[11]
(234)(567)(789)(99)[13]