Guest User

Untitled

a guest
Oct 6th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. using namespace std;
  11. typedef long long i64;
  12.  
  13. const int MAX_N = 200005;
  14. const int MAX_L = 1000005;
  15. int tmp[MAX_L];
  16. string s;
  17. string t;
  18. vector<int> pos[MAX_N];
  19. vector<int> pl;
  20. int early[MAX_N];
  21. int late[MAX_N];
  22. int ans[2][MAX_L];
  23.  
  24. int ToNum(const string &s) {
  25.     stringstream ss;
  26.     ss.str(s);
  27.     int res;
  28.     ss >> res;
  29.     return res;
  30. }
  31.  
  32. int main() {
  33. #ifdef NOVACO
  34.     freopen("input.txt", "r", stdin);
  35.     freopen("output.txt", "w", stdout);
  36. #else
  37.     freopen("math.in", "r", stdin);
  38.     freopen("math.out", "w", stdout);
  39. #endif
  40.     getline(cin, s);
  41.     int n = s.size();
  42.     for (int i = 0, j; i < n; i = j) {
  43.         if (s[i] == '^') {
  44.             tmp[i] = pl.size();
  45.             pl.push_back(i);
  46.             t = "";
  47.             j = i + 1;
  48.             while (j < n && isdigit(s[j])) {
  49.                 t += s[j];
  50.                 j++;
  51.             }
  52.             if (t.size() > 5) {
  53.                 continue;
  54.             }
  55.             int num = ToNum(t);
  56.             if (num >= MAX_N) {
  57.                 continue;
  58.             }
  59.             pos[num].push_back(i);
  60.         } else {
  61.             j = i + 1;
  62.         }
  63.     }
  64.     int last_pos = -1;
  65.     int max_n = 1;
  66.     while (true) {
  67.         int p = upper_bound(pos[max_n].begin(), pos[max_n].end(), last_pos) - pos[max_n].begin();
  68.         if (p == pos[max_n].size()) {
  69.             break;
  70.         }
  71.         early[max_n] = p;
  72.         last_pos = pos[max_n][p];
  73.         max_n++;
  74.     }
  75.     last_pos = n;
  76.     for (int j = max_n - 1; j >= 1; j--) {
  77.         int p = lower_bound(pos[j].begin(), pos[j].end(), last_pos) - pos[j].begin() - 1;
  78.         late[j] = p;
  79.         last_pos = pos[j][p];
  80.     }
  81.     memset(ans, -1, sizeof(ans));
  82.     for (int i = 1; i < max_n; i++) {
  83.         for (int j = 0; j < pos[i].size(); j++) {
  84.             if (j >= early[i] && j <= late[i]) {
  85.                 ans[0][tmp[pos[i][j]]] = 1;
  86.                 if (late[i] - early[i] > 0) {
  87.                     ans[1][tmp[pos[i][j]]] = 1;
  88.                 } else {
  89.                     ans[1][tmp[pos[i][j]]] = 0;
  90.                 }
  91.             } else {
  92.                 ans[0][tmp[pos[i][j]]] = 0;
  93.                 ans[1][tmp[pos[i][j]]] = 1;
  94.             }
  95.         }
  96.     }
  97.     for (int i = 0; i < pl.size(); i++) {
  98.         if (ans[0][tmp[pl[i]]] == -1) {
  99.             ans[0][tmp[pl[i]]] = 0;
  100.             ans[1][tmp[pl[i]]] = 1;
  101.         }
  102.     }
  103.     cout << pl.size() << "\n";
  104.     for (int i = 0; i < pl.size(); i++) {
  105.         cout << (ans[1][i] ? "Y" : "N");
  106.     }
  107.     cout << "\n";
  108.     for (int i = 0; i < pl.size(); i++) {
  109.         cout << (ans[0][i] ? "Y" : "N");
  110.     }
  111.     cout << "\n";
  112. }
Advertisement
Add Comment
Please, Sign In to add comment