fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // define
  5.  
  6. #define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
  7. #define ll long long
  8. #define ii pair <int , int>
  9. #define iii pair <int , ii>
  10. #define se second
  11. #define fi first
  12. #define all(v) (v).begin() , (v).end()
  13. #define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
  14. #define bit(x,i) (((x) >> (i)) & 1LL)
  15. #define flip(x,i) ((x) ^ (1LL << (i)))
  16. #define ms(d,x) memset(d , x , sizeof(d))
  17. #define exist __exist
  18. #define ends __ends
  19. #define visit visited
  20. #define left __left
  21. #define right __right
  22. #define prev __prev
  23. #define next __next
  24. #define sitingfake 1
  25. #define orz 1
  26. //constant
  27.  
  28. const long long mod = 1e9 + 7;
  29. const long long linf = 4557430888798830399LL;
  30. const long long nlinf = -4485090715960753727LL;
  31. const int inf = 1061109567;
  32. const int ninf = -1044266559;
  33. const int dx[] = {0 , -1 , 0 , 1};
  34. const int dy[] = {-1 , 0 , 1 , 0};
  35.  
  36. template<typename T> bool maximize(T &a, const T &b)
  37. {
  38. if(a < b) {a = b; return 1;}
  39. return 0;
  40. }
  41.  
  42. template<typename T> bool minimize(T &a, const T &b)
  43. {
  44. if(a > b) {a = b; return 1;}
  45. return 0;
  46. }
  47.  
  48. void Plus(ll & a ,ll b)
  49. {
  50. b %= mod;
  51. a += b;
  52. if(a < 0) a += mod;
  53. a %= mod;
  54. return;
  55. }
  56.  
  57. void Mul(ll & a, ll b)
  58. {
  59. (a *= (b % mod)) %= mod;
  60. return;
  61. }
  62.  
  63. //code
  64.  
  65. string num;
  66.  
  67. int x , y;
  68. int n;
  69. void solve(void)
  70. {
  71. cin >> num >> x >> y;
  72.  
  73. if(x > y) swap(x , y);
  74.  
  75. n = num.size();
  76.  
  77. if(n & 1)
  78. {
  79. if(x == 0)
  80. {
  81. cout << y;
  82. for(int i = 1; i <= (n + 1) / 2; i++)
  83. {
  84. cout << x;
  85. }
  86. for(int i = 1; i <= n / 2; i++)
  87. {
  88. cout << y;
  89. }
  90. }
  91. else
  92. {
  93. for(int i = 1; i <= (n + 1) / 2; i++)
  94. {
  95. cout << x;
  96. }
  97. for(int i = 1; i <= (n + 1) / 2; i++)
  98. {
  99. cout << y;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. int curX = n / 2;
  106. int curY = n / 2;
  107. int lastX = -1;
  108. int pos = 0;
  109. bool flag = 0;
  110. bool case1 = 0 , case2 = 0 , case3 = 0;
  111.  
  112. for(int i = 1; i <= n; i++)
  113. {
  114. int d = num[i - 1] - '0';
  115. if(curX && curY)
  116. {
  117. if(d == x)
  118. {
  119. lastX = i;
  120. curX--;
  121. }
  122. else if(d == y)
  123. {
  124. curY--;
  125. }
  126. else if(d < x)
  127. {
  128. case2 = 1;
  129. pos = i - 1;
  130. break;
  131. }
  132. else if(d < y)
  133. {
  134. case1 = 1;
  135. pos = i - 1;
  136. break;
  137. }
  138. else
  139. {
  140. case3 = 1;
  141. break;
  142. }
  143. }
  144. else if(curX)
  145. {
  146. if(d < x)
  147. {
  148. case2 = 1;
  149. pos = i - 1;
  150. break;
  151. }
  152. else if(d == x)
  153. {
  154. curX--;
  155. }
  156. else
  157. {
  158. case3 = 1;
  159. break;
  160. }
  161. }
  162. else
  163. {
  164. if(d < y)
  165. {
  166. case1 = 1;
  167. pos = i - 1;
  168. break;
  169. }
  170. else if(d == y)
  171. {
  172. curY--;
  173. }
  174. else
  175. {
  176. case3 = 1;
  177. break;
  178. }
  179. }
  180. if(i == n)
  181. {
  182. flag = 1;
  183. }
  184. }
  185. if(flag)
  186. {
  187. cout << num;
  188. }
  189. else if(case1)
  190. {
  191. int numx = n / 2 , numy = n / 2;
  192. for(int i = 1; i <= pos; i++)
  193. {
  194. int d = num[i - 1] - '0';
  195. if(d == x) numx--;
  196. if(d == y) numy--;
  197. cout << d;
  198. }
  199. cout << y;
  200. numy--;
  201. for(int i = 1; i <= numx; i++) cout << x;
  202. for(int i = 1; i <= numy; i++) cout << y;
  203. }
  204. else if(case2)
  205. {
  206. int numx = n / 2 , numy = n / 2;
  207. for(int i = 1; i <= pos; i++)
  208. {
  209. int d = num[i - 1] - '0';
  210. if(d == x) numx--;
  211. if(d == y) numy--;
  212. cout << d;
  213. }
  214. for(int i = 1; i <= numx; i++) cout << x;
  215. for(int i = 1; i <= numy; i++) cout << y;
  216. }
  217. else
  218. {
  219. if(lastX == -1)
  220. {
  221. if(x == 0)
  222. {
  223. cout << y;
  224. for(int i = 1; i <= (n + 2) / 2; i++) cout << x;
  225. for(int i = 1; i <= n / 2; i++) cout << y;
  226. }
  227. else
  228. {
  229. for(int i = 1; i <= (n + 2) / 2; i++) cout << x;
  230. for(int i = 1; i <= (n + 2) / 2; i++) cout << y;
  231. }
  232. }
  233. else
  234. {
  235. int numx = n / 2 , numy = n / 2;
  236.  
  237. for(int i = 1; i < lastX; i++)
  238. {
  239. int d = num[i - 1] - '0';
  240. if(d == x) --numx;
  241. if(d == y) --numy;
  242. cout << d;
  243. }
  244. cout << y;
  245. numy--;
  246. for(int i = 1; i <= numx; i++) cout << x;
  247. for(int i = 1; i <= numy; i++) cout << y;
  248. }
  249. }
  250. }
  251. cout << "\n";
  252. }
  253. /**
  254. **/
  255. signed main()
  256. {
  257. ios_base::sync_with_stdio(0);
  258. cin.tie(0);
  259. cout.tie(0);
  260.  
  261. #define task "digits"
  262.  
  263. if(fopen(task".inp","r"))
  264. {
  265. freopen(task".inp","r",stdin);
  266. freopen(task".out","w",stdout);
  267. }
  268.  
  269. int tc = 1;
  270. cin >> tc;
  271. while(tc--) solve();
  272.  
  273. // execute;
  274. }
  275.  
  276.  
  277.  
  278.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
00