fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char* bestdigs[10] = {"80.","9874310.","82.","983.","984.","9865.","86.","98730.","8.","98."};
  5.  
  6. char best_dig(char a, char b) {
  7. int i = 0;
  8. char *digs = bestdigs[a-48];
  9. while (digs[i] != '.') {
  10. if (digs[i] <= b) return digs[i];
  11. i++;
  12. }
  13. return '.';
  14. }
  15.  
  16. int main(void) {
  17. int t = 0;
  18. scanf("%d",&t);
  19. while (t--) {
  20.  
  21. char s[9], m[9];
  22. scanf("%s %s",&s,&m);
  23. int mymax = 0;
  24. int sl = strlen(s);
  25. int ml = strlen(m);
  26. int i;
  27.  
  28. // printf("\n\n\ns,m: %s,%s\n\n",&s,&m);
  29.  
  30. for (i = 0; i <= ml-sl; i++) {
  31. int j;
  32. char ans[9] = "--------";
  33. for (j = 0; j < i; j++) {
  34. ans[j] = m[j];
  35. }
  36. for (j = 0; j < sl; j++) {
  37. ans[j+i] = best_dig(s[j],m[j+i]);
  38.  
  39. // printf("a,s,m,i,j: '%s',%c,%c,%d,%d\n",ans,s[j],m[j+i],i,j);
  40.  
  41. if (ans[j+i]!=m[j+i]) {break;}
  42. }
  43. if (j < sl) {
  44. while (ans[j+i]=='.') {
  45. // printf("1: a,i,j: '%s', %d,%d\n",ans,i,j);
  46. j--;
  47. if (j < 0) {
  48. if (i == 0) {j = sl; break;}
  49. while (ans[j+i] == '0') {j--;}
  50. ans[j+i]--;
  51. j++;
  52. while (j<0) {ans[j+i] = '9'; j++;}
  53. j--;
  54. // printf("2: a,i,j: '%s', %d,%d\n",ans,i,j);
  55. break;
  56. }
  57. ans[j+i] = best_dig(s[j],m[j+i]-1);
  58. }
  59. j++;
  60. // printf("3: a,i,j: '%s', %d,%d\n",ans,i,j);
  61. while (i+j < ml) {
  62. ans[j+i] = 0;
  63. switch(s[j]){
  64. case '0':
  65. case '2':
  66. case '6':
  67. case '8':
  68. ans[j+i] -= 1;
  69. default:
  70. ans[j+i] += '9';
  71. }
  72. // printf("4: a,i,j: '%s', %d,%d\n",ans,i,j);
  73. j++;
  74. }
  75. } else {
  76. while (i+j < ml) {ans[j+i] = m[j+i]; j++;}
  77. }
  78. // printf("5: a,i,j: '%s', %d,%d\n",ans,i,j);
  79. int n = 0;
  80. for (j = -i; i+j < ml; j++) {
  81. n*=10; n+=ans[j+i]-48;
  82. }
  83. // printf("6: a,i,j: '%s', %d,%d,%d\n\n",ans,i,j,n);
  84. if (mymax < n) mymax = n;
  85. }
  86. printf("%d\n",mymax);
  87.  
  88. }
  89. return 0;
  90. }
  91. //707496 10257511 => 10099989
  92. //25 100 => 89
  93. //77 100 => 100
  94. //0 100 => 100
  95. //8 13 => 8
  96. //8 24 => 18
  97. //0 0 => 0
  98. //19 38 => 38
  99. //89375 9247529 => 9189999
  100. //804276 2857282 => 2809898
  101. //0 20130120 => 20130120
  102. //3284709 20130120 => 19889989
  103. //1 6 => 4
  104. //274 4883530 => 4883499
  105. //5 268343 => 268343
  106. //2 558870 => 558870
  107. //10381 16146 => 10989
  108. //0 6 => 0
  109. //0 9 => 8
  110. //2 200543 => 200543
  111. //4987565 14398964 => 9989989
  112. //1042216 1815366 => 1098898
  113. //7347946 12524098 => 10999998
  114. //2926618 18735670 => 12988898
  115.  
  116. // 24 707496 10257511 25 100 77 100 0 100 8 13 8 24 0 0 19 38 89375 9247529 804276 2857282 0 20130120 3284709 20130120 1 6 274 4883530 5 268343 2 558870 10381 16146 0 6 0 9 2 200543 4987565 14398964 1042216 1815366 7347946 12524098 2926618 18735670
Success #stdin #stdout 0s 2296KB
stdin
24
707496 10257511
25 100
77 100
0 100
8 13 
8 24 
0 0 
19 38 
89375 9247529 
804276 2857282 
0 20130120 
3284709 20130120 
1 6 
274 4883530 
5 268343 
2 558870 
10381 16146 
0 6 
0 9 
2 200543 
4987565 14398964 
1042216 1815366 
7347946 12524098 
2926618 18735670
stdout
10099989
89
100
100
8
18
0
38
9189999
2809898
20130120
19889989
4
4883499
268343
558870
10989
0
8
200543
9989989
1098898
10999998
12988898