Advertisement
aropan

snws [Round 1]. Special Dates

Jan 11th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int  months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30 ,31, 30, 31};
  10. const int MAXL = 20;
  11.  
  12. int d, m, y;
  13. string s;
  14. char c[MAXL];
  15. int k[MAXL];
  16.  
  17. int rec(string s, int n = 0)
  18. {
  19.     if (n == 3)
  20.     {
  21. //        cout << s << endl;
  22.         int n = s.size() - 1;
  23.         bool f = true;
  24.         for (int i = 1; i + i <= n; i++)
  25.             if (s[i] != s[n - i + 1])
  26.             {
  27.                 f = false;
  28.                 break;
  29.             }
  30.         if (f) return true;
  31.  
  32.         int x;
  33.         x = k[1] = 0;
  34.         for (int i = 2; i <= n; i++)
  35.         {
  36.             while (x && s[x + 1] != s[i]) x = k[x];
  37.             x += s[x + 1] == s[i];
  38.             k[i] = x;
  39.         }
  40.         if (x && n % (n - x) == 0) return true;
  41.  
  42.         return false;
  43.     }
  44.  
  45.     int x;
  46.     switch (n)
  47.     {
  48.         case 0:
  49.         case 1:
  50.             x = n == 0? d : m;
  51.             sprintf(c, "%02d", x);
  52.             if (rec(s + c, n + 1)) return true;
  53.             if (x < 10)
  54.             {
  55.                 sprintf(c, "%d", x);
  56.                 if (rec(s + c, n + 1)) return true;
  57.             }
  58.         break;
  59.  
  60.         case 2:
  61.             sprintf(c, "%d", y);
  62.             if (rec(s + c, n + 1)) return true;
  63.             sprintf(c, "%d", y % 100);
  64.             if (rec(s + c, n + 1)) return true;
  65.         break;
  66.     }
  67.  
  68.     return false;
  69. }
  70.  
  71. int main()
  72. {
  73.     freopen("special.in", "r", stdin);
  74.     freopen("special.out", "w", stdout);
  75.     scanf("%d %d %d", &d, &m, &y);
  76.     months[2] = 28 + (int)(y % 400 == 0 || y % 4 == 0 && y % 100 != 0);
  77.  
  78.     do
  79.     {
  80.         if (rec(" "))
  81.             break;
  82.  
  83.         d++;
  84.         if (months[m] < d)
  85.         {
  86.             d = 1;
  87.             m++;
  88.             if (12 < m)
  89.             {
  90.                 y++;
  91.                 m = 1;
  92.                 months[2] = 28 + (int)(y % 400 == 0 || y % 4 == 0 && y % 100 != 0);
  93.             }
  94.         }
  95.     } while (true);
  96.     printf("%d %d %d\n", d, m, y);
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement