Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. using namespace std;
  2. #include<bits/stdc++.h>
  3.  
  4. ifstream fin("unific.in");
  5. ofstream fout("unific.out");
  6.  
  7. int n;
  8. long long v[100001];
  9. int frcifre[10];
  10. int maximAppCifra;
  11. int cifMax;
  12. int i, k;
  13. long long nr, x;
  14.  
  15. void adaugaLaFrCifre(long long x) {
  16. if (x == 0) {
  17. frcifre[0]++;
  18. }
  19. while (x) {
  20. frcifre[x%10]++;
  21. x /= 10;
  22. }
  23. }
  24.  
  25. bool comun(long long a, long long b) {
  26.  
  27. bool fra[10];
  28. for (int c = 0; c<=9; c++) {
  29. fra[c] = 0;
  30. }
  31.  
  32. if (a == 0) {
  33. fra[0] = 1;
  34. } else {
  35. while (a) {
  36. fra[a%10] = 1;
  37. a /= 10;
  38. }
  39. }
  40.  
  41. if (b == 0 && fra[0] == 1) {
  42. return 1;
  43. }
  44.  
  45.  
  46. while (b) {
  47. if (fra[b%10] == 1) {
  48. return 1;
  49. }
  50. b /= 10;
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56. long long unifica(long long y, long long x) {
  57. short uz[10];
  58. long long p = 0;
  59. int UltimaCifraAdaugataY = 0;
  60. int nrcifreY = 0;
  61. for (int c = 0; c<=9; c++) {
  62. uz[c] = 0;
  63. }
  64.  
  65. long long z = y;
  66.  
  67. if (z == 0) {
  68. uz[0] = 1;
  69. } else {
  70. while (z > 0) {
  71. uz[z%10] = 1;
  72. z /= 10;
  73. }
  74. }
  75.  
  76. z = x;
  77. if (z == 0 && uz[0]) {
  78. uz[0] = 2;
  79. } else {
  80. p = 1;
  81. x = 0;
  82. while (z > 0) {
  83. short c = z%10;
  84. if (uz[c] == 0) {
  85. x = x + c*p;
  86. p *= 10;
  87. } else {
  88. uz[c] = 2;
  89. }
  90. z /= 10;
  91. }
  92. }
  93.  
  94.  
  95. if (y>0) {
  96. z = y;
  97. y = 0;
  98. p = 1;
  99.  
  100. while (z) {
  101. short c = z%10;
  102.  
  103. if (uz[c] == 1) {
  104. y = y + p*c;
  105. p *= 10;
  106. nrcifreY++;
  107. UltimaCifraAdaugataY = c;
  108. }
  109.  
  110. z /= 10;
  111. }
  112. }
  113.  
  114. if (x*y > 0) {
  115. if (UltimaCifraAdaugataY == 0) {
  116. while (p / 10 > y) {
  117. p /= 10;
  118. }
  119. }
  120.  
  121.  
  122. return x*p+y;
  123. } else {
  124. if (x > 0) {
  125. if (nrcifreY > 0) {
  126. x *= 10;
  127. }
  128.  
  129. return x;
  130. } else {
  131. if (nrcifreY == 0) {
  132. y = -1;
  133. }
  134.  
  135. return y;
  136. }
  137. }
  138. }
  139.  
  140. int main() {
  141.  
  142. fin >> n;
  143. fin >> v[1];
  144. adaugaLaFrCifre(v[1]);
  145.  
  146. i = 1;
  147. k = 1;
  148.  
  149. while (i<n) {
  150. fin >> v[++k];
  151. adaugaLaFrCifre(v[k]);
  152. i++;
  153.  
  154. while (comun(v[k-1], v[k]) && k > 1) {
  155. long long x = unifica(v[k], v[k-1]);
  156. if (x == -1) {
  157. k -= 2;
  158. } else {
  159. v[k-1] = x;
  160. k--;
  161. }
  162. }
  163. }
  164.  
  165. for (int j = 0; j<=9; j++) {
  166. if (frcifre[j] > maximAppCifra) {
  167. maximAppCifra = frcifre[j];
  168. cifMax = j;
  169. }
  170. }
  171.  
  172. fout << cifMax << endl << k << endl;
  173.  
  174. for (int j = 1; j<=k; j++) {
  175. fout << v[j] << " ";
  176. }
  177.  
  178.  
  179. fin.close();
  180. fout.close();
  181.  
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement