fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdint.h>
  4. #include <string>
  5. #include <tuple>
  6. #include <map>
  7. #include <vector>
  8. #include <functional>
  9. //var 0.03a
  10. enum class Op {
  11. None,
  12. Add,
  13. Sub,
  14. };
  15.  
  16. typedef std::map<char, std::uint32_t> Dic;
  17. typedef std::tuple<std::string, std::string, std::string, Op> Data;
  18. typedef std::vector<std::uint32_t> iVec;
  19. typedef std::vector<std::tuple<std::uint32_t, std::uint32_t, std::uint32_t,Op,Dic>> RType;
  20.  
  21. std::int32_t MakeNumber(std::string S, Dic& D) {
  22. std::int32_t R = 0;
  23.  
  24. for (auto& o : S) {
  25. R *= 10;
  26. R += D[o];
  27. }
  28.  
  29. return R;
  30. }
  31.  
  32. std::uint32_t CountDigit(std::uint32_t N) {
  33. std::uint32_t c = 0;
  34.  
  35. while (N != 0) {
  36. N /= 10;
  37. c++;
  38. }
  39. return c;
  40. }
  41.  
  42. RType MakeHoge(const Data& D){
  43. std::string A;
  44. std::string B;
  45. std::string C;
  46. Op op = Op::None;
  47. Dic Di;
  48. iVec v{ 0,1,2,3,4,5,6,7,8,9 };
  49. std::size_t i = 0;
  50. bool F=false;
  51. RType R;
  52.  
  53. std::int32_t a = 0;
  54. std::int32_t b = 0;
  55. std::int32_t c = 0;
  56.  
  57. std::tie(A, B, C, op) = D;
  58. for (auto& o : A) {
  59. Di[o] = 0;
  60. }
  61. for (auto& o : B) {
  62. Di[o] = 0;
  63. }
  64. for (auto& o : C) {
  65. Di[o] = 0;
  66. }
  67. do {
  68. for (auto& o : Di) {
  69. o.second = v[i];
  70. i++;
  71. }
  72. i = 0;
  73. a = MakeNumber(A, Di);
  74. b = MakeNumber(B, Di);
  75. c = MakeNumber(C, Di);
  76.  
  77. switch (op)
  78. {
  79. case Op::Add:
  80. F = (a + b == c);
  81. break;
  82. case Op::Sub:
  83. F = (a - b == c);
  84. break;
  85.  
  86. default:
  87. std::cout << "unknown op prametor" << std::endl;
  88. return{};
  89. break;
  90. }
  91. if (F == true) {
  92. R.push_back(std::make_tuple(a, b, c, op,Di));
  93. }
  94. } while (std::next_permutation(v.begin(), v.end()));
  95.  
  96. return R;
  97. }
  98.  
  99. bool Show(std::string A, std::string B, std::string C, RType& R) {
  100. std::int32_t a;
  101. std::int32_t b;
  102. std::int32_t c;
  103. Op op = Op::None;
  104. Dic D;
  105.  
  106. R.erase(std::unique(R.begin(), R.end()),R.end());
  107.  
  108. for (auto& o : R) {
  109. std::tie(a, b, c, op,D) = o;
  110.  
  111. if (A.size() != CountDigit(a)) continue;
  112. if (B.size() != CountDigit(b)) continue;
  113. if (C.size() != CountDigit(c)) continue;
  114.  
  115.  
  116. std::cout << A;
  117. switch (op)
  118. {
  119. case Op::Add:
  120. std::cout << " + ";
  121. break;
  122. case Op::Sub:
  123. std::cout << " - ";
  124. break;
  125. default:
  126. std::cout << " ? ";
  127. break;
  128. }
  129.  
  130. std::cout << B;
  131.  
  132. std::cout <<" = "<< C << std::endl;
  133. std::cout << a;
  134. switch (op)
  135. {
  136. case Op::Add:
  137. std::cout << " + ";
  138. break;
  139. case Op::Sub:
  140. std::cout << " - ";
  141. break;
  142. default:
  143. std::cout << " ? ";
  144. break;
  145. }
  146.  
  147. std::cout << b;
  148.  
  149. std::cout <<" = "<< c << std::endl;
  150. for (auto&o : D) {
  151. std::cout << o.first << '[' << static_cast<int>(o.second) << ']';
  152. }
  153. std::cout << std::endl<< std::endl;
  154. }
  155.  
  156. return true;
  157. }
  158.  
  159. int main() {
  160.  
  161. std::string A;
  162. std::string B;
  163. std::string C;
  164. Op op = Op::None;
  165. RType R;
  166.  
  167. A = "SEND";
  168. B = "MORE";
  169. C = "MONEY";
  170.  
  171. R = MakeHoge(std::make_tuple(A, B, C, Op::Add));
  172. Show(A, B, C, R);
  173.  
  174. A = "WWWDOT";
  175. B = "GOOGLE";
  176. C = "DOTCOM";
  177.  
  178. R = MakeHoge(std::make_tuple(A, B, C, Op::Sub));
  179. Show(A, B, C, R);
  180. return 0;
  181.  
  182. }
Success #stdin #stdout 4.73s 3284KB
stdin
Standard input is empty
stdout
SEND + MORE = MONEY
9567 + 1085 = 10652
D[7]E[5]M[1]N[6]O[0]R[8]S[9]Y[2]

WWWDOT - GOOGLE = DOTCOM
777589 - 188103 = 589486
C[4]D[5]E[3]G[1]L[0]M[6]O[8]T[9]W[7]

WWWDOT - GOOGLE = DOTCOM
777589 - 188106 = 589483
C[4]D[5]E[6]G[1]L[0]M[3]O[8]T[9]W[7]