首先会想到|x|是不递减的。

于是可以枚举长度L。

再每个L设一个断点,xx必定经过两个断点。

两两断点间求最长公共前后缀,这里用hash+二分会快。

然后一波扫过去就好了。

如果找到了,hash就要重构。

来计算一下复杂度。

一共有O(n log n)个断点,每个求最长公共前后缀复杂度log,这一部分是O(n log^2 n )

长度小于 \(L \leq n\)的最多 \(n\sqrt n\)次,\(L\geq n\)最多 \(n\sqrt n\)中,所以重构复杂度: \(O(n \sqrt n)\)

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #define ll long long
  5. #define fo(i, x, y) for(int i = x; i <= y; i ++)
  6. #define min(a, b) ((a) < (b) ? (a) : (b))
  7. #define P pair<int, int>
  8. using namespace std;
  9. const int N = 50005, mo = 998244353, pri = 1e9 + 7, pri2 = 43313;
  10. char str[N];
  11. int n, bz[N], tmp;
  12. ll c[N], ni[N], s[N];
  13. ll ksm(ll x, ll y) {
  14. ll s = 1;
  15. for(; y; x = x * x % mo, y >>= 1)
  16. if(y & 1) s = s * x % mo;
  17. return s;
  18. }
  19. void Getsum() {
  20. fo(i, 1, n) s[i] = (s[i - 1] + c[i] * (str[i] - 'a') % mo) % mo;
  21. }
  22. int sum(int x, int y) {
  23. return ((s[y] - s[x - 1] + mo) * ni[x] % mo);
  24. }
  25. int Getq(int x, int y) {
  26. int ans = 0;
  27. for(int l = 1, r = tmp; l <= r;) {
  28. int m = l + r >> 1;
  29. if(sum(x - m + 1, x) == sum(y - m + 1, y))
  30. ans = m, l = m + 1; else r = m - 1;
  31. }
  32. return ans;
  33. }
  34. int Geth(int x, int y) {
  35. int ans = 0;
  36. for(int l = 1, r = n - y + 1; l <= r;) {
  37. int m = l + r >> 1;
  38. if(sum(x, x + m - 1) == sum(y, y + m - 1))
  39. ans = m, l = m + 1; else r = m - 1;
  40. }
  41. return ans;
  42. }
  43. int main() {
  44. scanf("%s", str + 1); n = strlen(str + 1);
  45. c[0] = ni[0] = 1;
  46. ni[1] = ksm(pri, mo - 2); c[1] = pri;
  47. fo(i, 2, n) ni[i] = ni[i - 1] * ni[1] % mo, c[i] = c[i - 1] * c[1] % mo;
  48. int n0 = n; Getsum();
  49. fo(l, 1, n) {
  50. int xg = 0; tmp = l;
  51. fo(i, 1, n / l) {
  52. int x = i * l, y = x + l;
  53. if(y > n) break;
  54. int q = Getq(x, y), h = Geth(x, y);
  55. if(q + h > l) {
  56. fo(j, x - q + 1, x - q + l) bz[j] = l;
  57. tmp = q;
  58. xg = 1;
  59. } else tmp = l;
  60. }
  61. if(xg) {
  62. int n1 = 0;
  63. fo(i, 1, n) if(bz[i] != l)
  64. str[++ n1] = str[i];
  65. n = n1;
  66. Getsum();
  67. }
  68. }
  69. fo(i, 1, n) putchar(str[i]);
  70. }

Codeforces 319D Have You Ever Heard About the Word?的更多相关文章

  1. CF 319D(Have You Ever Heard About the Word?-模拟)

    D. Have You Ever Heard About the Word? time limit per test 6 seconds memory limit per test 256 megab ...

  2. Codeforce 水题报告

    最近做了好多CF的题的说,很多cf的题都很有启发性觉得很有必要总结一下,再加上上次写题解因为太简单被老师骂了,所以这次决定总结一下,也发表一下停课一星期的感想= = Codeforces 261E M ...

  3. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  4. Codeforces Round #189 (Div. 1 + Div. 2)

    A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...

  5. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  6. CodeForces - 426A(排序)

    Sereja and Mugs Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  7. Codeforces Gym 100803D Space Golf 物理题

    Space Golf 题目连接: http://codeforces.com/gym/100803/attachments Description You surely have never hear ...

  8. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  9. codeforces Gym 100187H H. Mysterious Photos 水题

    H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

随机推荐

  1. 京东面试题 Java相关

    1.JVM的内存结构和管理机制: JVM实例:一个独立运行的java程序,是进程级别 JVM执行引擎:用户运行程序的线程,是JVM实例的一部分 JVM实例的诞生 当启动一个java程序时.一个JVM实 ...

  2. T61

    你参加了这次科学讨论会,有什么体会?What have you learned from the symposium?那墙有点斜.The wall is a little out of the per ...

  3. L91

    Make Healthy Choices Easier Options Telling people to change unhealthy behaviors doesn't work. Other ...

  4. codeforces 705C C. Thor(模拟)

    题目链接: C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. hdu-5784 How Many Triangles(计算几何+极角排序)

    题目链接: How Many Triangles Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  6. Web 攻击之 XSS 攻击及防御策略

    XSS 攻击 介绍 XSS 攻击,从最初 netscap 推出 javascript 时,就已经察觉到了危险. 我们常常需要面临跨域的解决方案,其实同源策略是保护我们的网站.糟糕的跨域会带来危险,虽然 ...

  7. MySQL-left join _20160928

    left join 左连接 返回from 后面表的全部记录和 left join 后面表和from 后面表条件相符的全部记录 一般格式为下面,首先table A 和tableB都有两个唯一的字段标识I ...

  8. 【LeetCode】051. N-Queens

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  9. AtCoder Regular Contest 072 E:Alice in linear land

    题目传送门:https://arc072.contest.atcoder.jp/tasks/arc072_c 题目翻译 给你一个数组\(D\),然后给你一个操作序列\(d\),每次操作可以将\(D\) ...

  10. BZOJ3123:[SDOI2013]森林

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...