Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <string>
  6. #include <unordered_map>
  7. #include <unordered_set>
  8. #include <map>
  9. #include <set>
  10. #include <queue>
  11. #include <ostream>
  12. #include <istream>
  13. #include <typeinfo>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cassert>
  18. #include <limits>
  19. #include <fstream>
  20. #include <array>
  21. #include <list>
  22. #include <bitset>
  23. #include <functional>
  24.  
  25. #define mt make_tuple
  26. #define x first
  27. #define y second
  28. #define pb push_back
  29. #define ppb pop_back
  30. #define pf push_front
  31. #define ppf pop_front
  32. #define mp make_pair
  33. #define umap unordered_map
  34. #define uset unordered_set
  35. #define rt return 0;
  36. #define elif else if
  37. #define len(v) ((int)v.size())
  38.  
  39.  
  40. using namespace std;
  41.  
  42. char string_in_buffer[(int)2e6];
  43.  
  44.  
  45. void fast_scan(int &x) { scanf("%d", &x); }
  46. void fast_scan(long long &x) { scanf("%lld", &x); }
  47. void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
  48. void fast_scan(double &x) { scanf("%lf", &x); }
  49. void fast_scan(long double &x) { scanf("%Lf", &x); }
  50. void fast_scan(char &x) {
  51. scanf("%c", &x);
  52. if (x == '\n') {
  53. fast_scan(x);
  54. }
  55. }
  56. void fast_scan(string &x) {
  57. scanf("%s", string_in_buffer);
  58. x = string(string_in_buffer);
  59. }
  60.  
  61. template<class TFirst, class TSecond>
  62. void fast_scan(pair<TFirst, TSecond> &p) {
  63. fast_scan(p.first);
  64. fast_scan(p.second);
  65. }
  66.  
  67. template <class T>
  68. void fast_scan(vector<T> &v) {
  69. for (auto &x : v) fast_scan(x);
  70. }
  71.  
  72. void fast_print(const int &x) { printf("%d", x); }
  73. void fast_print(const long long &x) { printf("%lld", x); }
  74. void fast_print(const unsigned long long &x) { printf("%llu", x); }
  75. void fast_print(const double &x) { printf("%.15lf", x); }
  76. void fast_print(const long double &x) { printf("%.15Lf", x); }
  77. void fast_print(const char &x) { printf("%c", x); };
  78. void fast_print(const string &x) { printf("%s", x.c_str());}
  79.  
  80. template<class TFirst, class TSecond>
  81. void fast_print(const pair<TFirst, TSecond> &p) {
  82. fast_print(p.first);
  83. fast_print(' ');
  84. fast_print(p.second);
  85. }
  86.  
  87. template <class T>
  88. void fast_print(const vector<T> &v) {
  89. if (v.empty()) return;
  90. fast_print(v[0]);
  91. for (int i = 1; i < v.size(); i++) {
  92. fast_print(" ");
  93. fast_print(v[i]);
  94. }
  95. }
  96.  
  97. template <class T>
  98. void fast_print(const vector<vector<T>> &v) {
  99. if (v.empty()) return;
  100. fast_print(v[0]);
  101. for (int i = 1; i < v.size(); i++) {
  102. fast_print("\n");
  103. fast_print(v[i]);
  104. }
  105. }
  106.  
  107.  
  108.  
  109. using namespace std;
  110.  
  111.  
  112. namespace smart_io {
  113. string print_start = "";
  114. string sep = " ";
  115. bool first_print = false;
  116.  
  117. void precall_print() {
  118. fast_print(print_start);
  119. print_start = "\n";
  120. first_print = true;
  121. }
  122. } //namespace smart_io
  123.  
  124.  
  125. template <class T>
  126. ostream &operator,(ostream &os, const T &object) {
  127. if (!smart_io::first_print) {
  128. fast_print(smart_io::sep);
  129. } else {
  130. smart_io::first_print = false;
  131. }
  132. fast_print(object);
  133. return os;
  134. }
  135.  
  136. template <class T>
  137. istream &operator,(istream &is, T &object) {
  138. fast_scan(object);
  139. return is;
  140. }
  141.  
  142. namespace typedefs {
  143. typedef long long ll;
  144. typedef unsigned long long ull;
  145. typedef pair<int, int> pii;
  146. }
  147.  
  148. namespace numbers_operation {
  149. template<class T>
  150. T floor_mod(T a, T b) {
  151. if (a % b == 0) return 0;
  152. if (a >= 0 && b >= 0) return a % b;
  153. if (a <= 0 && b <= 0) return a % b;
  154. return abs(b) - (abs(a) % abs(b));
  155. }
  156. }
  157.  
  158. using namespace numbers_operation;
  159. using namespace typedefs;
  160.  
  161. #define print \
  162. smart_io::precall_print(); \
  163. cout,
  164.  
  165. #define scan cin,
  166.  
  167.  
  168. template <class T, class T_Magic>
  169. class SparseTable {
  170. public:
  171. T_Magic magic;
  172. vector<int> two_powers{1};
  173. vector<int> max_pow2;
  174. vector<vector<T>> tree;
  175. private:
  176. int next_2log(int n) {
  177. int x = log2(n);
  178. if (n != pow(x, 2)) {
  179. x++;
  180. }
  181. return x;
  182. }
  183.  
  184. T __query(int left, int right) {
  185. int block = max_pow2[right - left + 1];
  186. return magic(tree[block][left], tree[block][right - (two_powers[block]) + 1]);
  187. }
  188. public:
  189. SparseTable(vector<T> &v, T_Magic magic):magic(magic) {
  190. int n = v.size();
  191. int logn = log2(n) + 1;
  192.  
  193. if (two_powers.empty()) {
  194. two_powers.pb(1);
  195. }
  196. while (two_powers.size() <= logn) {
  197. two_powers.pb(2 * two_powers[two_powers.size() - 1]);
  198. }
  199.  
  200. // max_pow2 construction
  201. max_pow2.resize(n + 1, 1);
  202. int cur_pow = 1;
  203. int cur_log = 0;
  204. for (int i = 0; i < n + 1; i++) {
  205. if (i >= cur_pow) {
  206. cur_pow *= 2;
  207. cur_log++;
  208. }
  209. max_pow2[i] = max(cur_log - 1, 0);
  210. }
  211. // cout << max_pow2 << nl;
  212. // exit(0);
  213. tree.resize(logn, vector<T> (n));
  214. tree[0] = v;
  215. construct_tree();
  216. };
  217.  
  218. void construct_tree() {
  219. for (int i = 1; i < tree.size(); i++) {
  220. for (int j = 0; j < tree[i].size(); j++) {
  221. int next_block = j + two_powers[i - 1];
  222. tree[i][j] = tree[i - 1][j];
  223. if (next_block < tree[i - 1].size()) {
  224. tree[i][j] = magic(tree[i][j], tree[i - 1][next_block]);
  225. }
  226. }
  227. }
  228. }
  229.  
  230. // calcs magic in [L:R)
  231. T query(int left, int right) {
  232. right--;
  233. if (left > right) {
  234. tie(left, right) = mt(right, left);
  235. }
  236. return __query(left, right);
  237. }
  238.  
  239. };
  240.  
  241.  
  242. vector<int> get_p(const string &s) {
  243. int n = len(s);
  244. vector<int> pref(n);
  245. for (int i = 0; i < n; i++) {
  246. pref[i] = (s[i] == 'j') ? -1 : 1;
  247. if (i > 0) {
  248. pref[i] += pref[i - 1];
  249. }
  250. }
  251. auto magic = [](const int &a, const int &b){
  252. return min(a, b);
  253. };
  254. SparseTable<int, decltype(magic)> sp(pref, magic);
  255. vector<int> p(n, 0);
  256. for (int i = 0; i < n; i++) {
  257. if (s[i] == 'j') {
  258. p[i] = i;
  259. continue;
  260. }
  261. int pr = (i == 0) ? 0 : pref[i - 1];
  262. int left = i + 1;
  263. int right = n + 1;
  264. while (right - left > 1) {
  265. int mid = (left + right) / 2;
  266. if (sp.query(i, mid) >= pr) {
  267. left = mid;
  268. } else {
  269. right = mid;
  270. }
  271. }
  272. p[i] = left;
  273. }
  274. return p;
  275. }
  276.  
  277. signed main(signed argc, char *argv[]) {
  278. int n;
  279. scan n;
  280. string s;
  281. scan s;
  282.  
  283. auto p = get_p(s);
  284. reverse(s.begin(), s.end());
  285. auto q = get_p(s);
  286. reverse(s.begin(), s.end());
  287. reverse(q.begin(), q.end());
  288. for (int i = 0; i < n; i++) {
  289. q[i] = n - q[i];
  290. }
  291. // print q;
  292.  
  293. auto magic = [](const int &a, const int &b) {
  294. return min(a, b);
  295. };
  296. int _max = 0;
  297. SparseTable<int, decltype(magic)> sp(q, magic);
  298. for (int i = 0; i < n; i++) {
  299. if (p[i] == i) continue;
  300. int r = p[i] - 1;
  301. int left = -1;
  302. int right = (r - i);
  303. while (right - left > 1) {
  304. int mid = (left + right) / 2;
  305. if (sp.query(r - mid, r + 1) <= i) {
  306. right = mid;
  307. } else {
  308. left = mid;
  309. }
  310. }
  311. _max = max(_max, (r - right) - i + 1);
  312. }
  313. print _max;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement