概念

用hash求最长回文串/回文串数

首先,易知,回文串具有单调性。

如果字符串 $s[l...r]$ 为回文串串,那么 $s[x...y]$($l < x, y < r$ 且 $|l-x| = |r-y|$)也一定是回文串。

因此,可以二分。

通常,枚举一下起点或者中点,然后二分长度。

这样复杂度为 $O(nlogn)$,逊色于马拉车 $O(n)$,但在时限不那么紧的情况下,hash也是不错的选择。

例题

题意:对于一个 0/1 串,如果将这个字符串 0 和 1 取反后,再将整个串反过来和原串一样,就称作「反对称」字符串。先给出一个长为 $n$ 的 0/1 串,求它有多少反对称字串。($1 \leq n\leq 500000$)

分析:

先正向求一遍hash,再反向求一遍hash(0,1互换)。

枚举中点,判断左右两半的hash值是否相同。

显然,不存在长度为奇数的反对称串。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef unsigned long long ull;
  5. typedef long long ll;
  6. const ull base = ;
  7. const int maxn = 5e5 + ;
  8.  
  9. ull h1[maxn], h2[maxn], p[maxn];
  10. int n;
  11. ll ans;
  12. char s[maxn];
  13.  
  14. ull get_h1(int l, int r)
  15. {
  16. return h1[r] - h1[l-]*p[r-l+];
  17. }
  18. ull get_h2(int l, int r)
  19. {
  20. return h2[l] - h2[r+]*p[r-l+];
  21. }
  22.  
  23. int check(int x)
  24. {
  25. int l=, r = min(x, n-x);
  26. int ans=;
  27. while(l <= r)
  28. {
  29. int mid = (l+r) >> ;
  30. if(get_h1(x-mid+,x) == get_h2(x+, x+mid))
  31. {
  32. ans = mid;
  33. l = mid + ;
  34. }
  35. else r = mid - ;
  36. }
  37. return ans;
  38. }
  39.  
  40. int main()
  41. {
  42. scanf("%d%s", &n, s+);
  43. p[] = ;
  44. for(int i = ;i <= n;i++)
  45. {
  46. p[i] = p[i-]*base;
  47. h1[i] = h1[i-]*base + (ull)s[i];
  48. }
  49. for(int i = n;i >= ;i--) h2[i] = h2[i+]*base + (ull)(s[i] == '' ? s[i]+ : s[i]-);
  50.  
  51. for(int i = ;i < n;i++) ans += check(i);
  52. printf("%lld\n", ans);
  53. return ;
  54. }

参考链接:https://www.cnblogs.com/henry-1202/p/10321013.html

LOJ 2452 对称 Antisymmetry——用hash求回文串数的更多相关文章

  1. SPOJ STC02 - Antisymmetry(Manacher算法求回文串数)

    http://www.spoj.com/problems/STC02/en/ 题意:给出一个长度为n的字符串,问其中有多少个子串s可以使得s = s按位取反+翻转. 例如样例:11001011. 10 ...

  2. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  3. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

  4. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...

  5. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  6. manacher算法,求回文串

    用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...

  7. hdu 3294 manacher 求回文串

    感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...

  8. hihocoder 1032 manachar 求回文串O(n)

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  9. 拓展KMP求回文串

    题目:hdu3613: 题意:有26字母对应的价值,然后给出以个串,把它分成两段字串,如果字串是回文串,串的价值就是每个字符和,不是就为0.求最大价值. 博客 分析:拓展KMP的应用求回文字串. #i ...

随机推荐

  1. Prometheus入门到放弃(2)之Node_export安装部署

    1.下载安装 node_exporter服务需要在三台机器都安装,这里我们以一台机器为例: 地址:https://prometheus.io/download/ ### 另外两个节点部署时,需要先创建 ...

  2. go 食用指南

    Golang高效食用秘籍 一.关于构建 1.1 go环境变量 $ go env // 查看go 的环境变量 其中 GOROOT 是golang 的安装路径 GOPATH 是go命令依赖的一个环境变量 ...

  3. QueryList::Query() The received content is empty!的经准灵活解决办法

    QueryList::Query() The received content is empty! 最近因为项目问题出现The received content is empty!,我也有过在网上寻找 ...

  4. 【HC89S003F4开发板】 2调度器

    HC89S003F4开发板调度器调试 一.前言 逛论坛看到有人给HC89做了调度器,调度部分直接操作堆栈的. 下载链接 下载下来调试看看. 二.用户代码 @实现效果 编译后led灯闪烁 #includ ...

  5. 怎样重启ssh服务

    尝试下面两个命令: service sshd restart systemctl restart sshd.service

  6. javascript 之 命名空间

    注意点: 1/IIFE是现代js框架最主要的基础设施,保证变量不被污染. 2/基本上我们把命名空间等同于框架的名字. 内容 一.简单的命名空间 <input type="button& ...

  7. linux高频操作: host,用户管理,免密登陆,管道,文件权限,脚本,防火墙,查找

    1. 修改hosts和hostname 2. 用户管理 3. 免秘登陆 4. 文件末尾添加 >> 5. 设置可执行文件 6. 任何地方调用 7. Centos6 永久关闭防火墙 8. Ce ...

  8. VBA Exit Do语句

    当想要根据特定标准退出Do循环时,可使用Exit Do语句. 它可以同时用于Do...While和Do...Until直到循环. 当Exit Do被执行时,控制器在Do循环之后立即跳转到下一个语句. ...

  9. webpack资源加载常用配置

    const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundl ...

  10. dao 接口定义了一个方法,报错 The method xxx is undefined for the type xxx;

    转自:https://blog.csdn.net/panshoujia/article/details/78203837 持久层(DAO层)下的一个接口 ,eclipse报了一个The method ...