Guest User

Untitled

a guest
Dec 7th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #define MAXN 105
  5.  
  6. static char a[MAXN];
  7. int N, D;
  8. static char num[MAXN];
  9. static char b[MAXN];
  10.  
  11. static int q[MAXN];
  12. int qc;
  13.  
  14. void solve (int point)
  15. {
  16.     if (point == 5)
  17.     {
  18.         qc = 0;
  19.         int pos = 0;
  20.         while (pos < N)
  21.         {
  22.             if (num[pos]) pos++;
  23.             else
  24.             {
  25.                 if (a[pos] > '5')
  26.                 {
  27.                     int what = (int)(a[pos] - '0');
  28.                     if ((what < 6) || (what > D) || (b[what]))
  29.                     {
  30.                         for (int i = 0; i < qc; i++) b[q[i]] = 0;
  31.                         return;
  32.                     }
  33.                     b[what] = 1;
  34.                     q[qc++] = what;
  35.                     pos++;
  36.                 }
  37.                 else
  38.                 {
  39.                     if ((pos + 1) == N)
  40.                     {
  41.                         for (int i = 0; i < qc; i++) b[q[i]] = 0;
  42.                         return;
  43.                     }
  44.                     int what = (int)(a[pos] - '0') * 10 + (int)(a[pos+1] - '0');
  45.                     if ((what < 6) || (what > D) || (b[what]))
  46.                     {
  47.                         for (int i = 0; i < qc; i++) b[q[i]] = 0;
  48.                         return;
  49.                     }
  50.                     b[what] = 1;
  51.                     q[qc++] = what;
  52.                     pos += 2;
  53.                 }
  54.             }
  55.         }
  56.         pos = 0;
  57.         char first = 1;
  58.         while (pos < N)
  59.         {
  60.             if (first) first = 0;
  61.             else printf(" ");
  62.             if (num[pos]) printf("%c", a[pos++]);
  63.             else if (a[pos] > '5') printf("%c", a[pos++]);
  64.             else
  65.             {
  66.                 printf("%c", a[pos++]);
  67.                 printf("%c", a[pos++]);
  68.             }
  69.         }
  70.         printf("\n");
  71.         exit(0);
  72.     }
  73.     else
  74.     {
  75.         int i;
  76.         for (i = 0; i < N; i++)
  77.         {
  78.             if (a[i] == (char)(point) + '1')
  79.             {
  80.                 num[i] = 1;
  81.                 solve(point+1);
  82.                 num[i] = 0;
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. int main ()
  89. {
  90.     scanf("%s",a);
  91.     N = strlen(a);
  92.     if (N < 10)
  93.     {
  94.         printf("%c",a[0]);
  95.         for (int i = 1; i < N; i++) printf(" %c",a[i]);
  96.         printf("\n");
  97.         return 0;
  98.     }
  99.     D = (N - 9) / 2 + 9;
  100.     memset(b,0,sizeof(b));
  101.     solve(0);
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment