Advertisement
Guest User

Perfect Separating

a guest
Jun 13th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 77;
  6. const int G = 30;
  7.  
  8. int n;
  9. char s[N];
  10.  
  11. int half;
  12.  
  13. map<int, int> cnt[N];
  14. long long ans;
  15.  
  16. int suf(int mask, int i) {
  17.     return mask & ((1 << i) - 1);
  18. }
  19.  
  20. void goLeft(int i, int sx, int sy, int x, int y) {
  21.     if (i == half) {
  22.         if (sx <= sy) {
  23.             if (x != suf(y, sx)) return;
  24.             ++cnt[sx - sy + G][y >> sx];
  25.         } else {
  26.             if (y != suf(x, sy)) return;
  27.             ++cnt[sx - sy + G][x >> sy];
  28.         }
  29.         return;
  30.     }
  31.     goLeft(i + 1, sx + 1, sy, x | (int(s[i]) << sx), y);
  32.     goLeft(i + 1, sx, sy + 1, x, y | (int(s[i]) << sy));
  33. }
  34.  
  35. void goRight(int i, int sx, int sy, int x, int y) {
  36.     if (i < half) {
  37.         if (sx <= sy) {
  38.             if (x != (y >> (sy - sx))) return;
  39.             ans += cnt[sy - sx + G][suf(y, sy - sx)];
  40.         } else {
  41.             if (y != (x >> (sx - sy))) return;
  42.             ans += cnt[sy - sx + G][suf(x, sx - sy)];
  43.         }
  44.         return;
  45.     }
  46.     goRight(i - 1, sx + 1, sy, x << 1 | s[i], y);
  47.     goRight(i - 1, sx, sy + 1, x, y << 1 | s[i]);
  48. }
  49.  
  50. int main() {
  51.     cin >> s;
  52.     n = strlen(s);
  53.     half = n / 2;
  54.     for (int i = 0; i < n; ++i) s[i] -= 'a';
  55.  
  56.     goLeft(0, 0, 0, 0, 0);
  57.     goRight(n - 1, 0, 0, 0, 0);
  58.  
  59.     cout << ans << endl;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement