Advertisement
bibaboba12345

Untitled

Jan 20th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #pragma GCC target("avx2")
  3. #pragma GCC optimize("Ofast")
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <string>
  7. #include <cmath>
  8. #include <iomanip>
  9. #include <vector>
  10. #include <set>
  11. #include <map>
  12.  
  13. using namespace std;
  14. const int N = 1e6 + 7, M = 1e9 + 7;
  15.  
  16. struct Jump {
  17. int l, r;
  18. };
  19.  
  20. int n, l, r;
  21. string s;
  22.  
  23. Jump jumps[N], obn[N];
  24.  
  25. char fst, sec;
  26. int fstc, secc, PREF[N][26];
  27.  
  28. int get(int c, int l, int r) {
  29. if (l == 0) {
  30. return PREF[r][c];
  31. }
  32. return PREF[r][c] - PREF[l - 1][c];
  33. }
  34.  
  35. bool good(int l, int r) {
  36. int cnt = 0;
  37. if (l == 0) {
  38. for (int c = 0; c < 26; c++) {
  39. if (PREF[r][c]) {
  40. cnt++;
  41. }
  42. }
  43. }
  44. else {
  45. for (int c = 0; c < 26; c++) {
  46. if (PREF[r][c] - PREF[l - 1][c]) {
  47. cnt++;
  48. }
  49. }
  50. }
  51. return cnt == 2;
  52. }
  53.  
  54. bool vis[N];
  55. void dfs(int v) {
  56. vis[v] = 1;
  57. if (jumps[v].l == -1) {
  58. return;
  59. }
  60. for (int i = jumps[v].l + 1; i <= jumps[v].r + 1; i++) {
  61. if (!vis[i]) {
  62. dfs(i);
  63. }
  64. }
  65. }
  66.  
  67. int dp[N];
  68.  
  69. void prepare() {
  70. n = s.size();
  71. for (int i = 0; i < n; i++) {
  72. for (int j = 0; j < 26; j++) {
  73. PREF[i][j] = 0;
  74. }
  75. }
  76. for (int i = 0; i < n; i++) {
  77. PREF[i][s[i] - 'a']++;
  78. if (i != 0) {
  79. for (int j = 0; j < 26; j++) {
  80. PREF[i][j] += PREF[i - 1][j];
  81. }
  82. }
  83. }
  84. for (int i = 0; i < n; i++) {
  85. if (i == 0) {
  86. fst = s[i];
  87. fstc = 0;
  88. int j = i;
  89. while (j != n && s[j] == fst) {
  90. j++;
  91. fstc++;
  92. }
  93. if (j == n) {
  94. cout << "chastniy sluchay - " << s << "\n";
  95. return;
  96. }
  97. jumps[i].l = j;
  98. sec = s[j];
  99. secc = 0;
  100. while (j != n && (s[j] == sec || s[j] == fst)) {
  101. j++;
  102. if (s[j] == fst) {
  103. fstc++;
  104. }
  105. else {
  106. secc++;
  107. }
  108. }
  109. jumps[i].r = j - 1;
  110. l = jumps[i].l;
  111. r = jumps[i].r;
  112. }
  113. else {
  114. if (s[i - 1] == fst) {
  115. fstc--;
  116. }
  117. else {
  118. secc--;
  119. }
  120. if (i == 6) {
  121. cout << "";
  122. }
  123. if (!good(i, l)) {
  124. l++;
  125. while (!good(i, l) && l < n) {
  126. l++;
  127. }
  128. if (l >= n) {
  129. jumps[i].r = -1;
  130. jumps[i].l = -1;
  131. }
  132. else {
  133. jumps[i].l = l;
  134. r = max(r, l);
  135. while (good(i, r) && r < n) {
  136. r++;
  137. }
  138. r--;
  139. jumps[i].r = r;
  140. }
  141. continue;
  142. }
  143. jumps[i].l = l;
  144. jumps[i].r = r;
  145. }
  146. }
  147. jumps[n] = { -1,-1 };
  148. }
  149.  
  150. long long stupid_solve() {
  151. long long ANS = 0;
  152. for (int i = 0; i < n; i++) {
  153. if (jumps[i].l == -1) {
  154. continue;
  155. }
  156. for (int j = i; j <= n; j++) {
  157. vis[j] = 0;
  158. }
  159. for (int j = jumps[i].l + 1; j <= jumps[i].r + 1; j++) {
  160. dfs(j);
  161. }
  162. for (int j = i; j <= n; j++) {
  163. if (vis[j]) {
  164. ANS++;
  165. }
  166. }
  167. }
  168. return ANS;
  169. }
  170.  
  171. long long clever_solve() {
  172. long long ANS = 0;
  173. for (int i = 0; i <= n + 2; i++) {
  174. dp[i] = 0;
  175. }
  176. for (int i = 1; i < n; i++) {
  177. if (s[i] == s[i - 1]) {
  178. dp[i] = dp[i - 1];
  179. }
  180. else {
  181. if (i > 1 && good(i - 2, i)) {
  182. dp[i] = i;
  183. }
  184. else {
  185. if (i > 1) {
  186. dp[i] = 1 + dp[i - 2];
  187. }
  188. else {
  189. dp[i] = 1;
  190. }
  191. }
  192. }
  193. ANS += dp[i];
  194. }
  195. return ANS;
  196. }
  197.  
  198. string randomize() {
  199. int n = 200;
  200. string s;
  201. for (int i = 0; i < n; i++) {
  202. char c = 'a' + rand() % 26;
  203. if (s.size() != 0) {
  204. while (c == s[s.size() - 1]) {
  205. c = 'a' + rand() % 26;
  206. }
  207. }
  208. s.push_back('a' + rand() % 26);
  209. }
  210. return s;
  211. }
  212.  
  213. signed main()
  214. {
  215. #ifdef _DEBUG
  216. freopen("input.txt", "r", stdin);
  217. //freopen("trains.out", "w", stdout);
  218. #else
  219. //freopen("sequence.in", "r", stdin);
  220. //freopen("sequence.out", "w", stdout);
  221. #endif
  222. for (int I = 0; I < 20000; I++) {
  223. //s = randomize();
  224. cin >> s >> s;
  225. prepare();
  226. //long long S_ANS = stupid_solve();
  227. long long C_ANS = clever_solve();
  228. cout << C_ANS;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement