hiho1602本质不同的回文子串的数量
给定一个字符串S,请统计S的所有子串中,有多少个本质不同的回文字符串?
注意如果两个位置不同的子串满足长度相同且对应字符也都相同,则认为这两个子串本质上是相同的。
Input
一个只包含小写字母的字符串S。
对于30%的数据,S长度不超过100。
对于60%的数据,S长度不超过1000。
对于100%的数据,S长度不超过800000。
Output
回文子串的数量
Sample Input
abbab
Sample Output
5
马拉车算法+bkrdhash:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define ULL unsigned long long
const int maxn=+;
ULL P = ;
ULL sqr[maxn/],has[maxn/],V[maxn]; int Laxt[maxn],Next[maxn],cnt=; const int MOD = ; bool _insert(ULL Now)
{
int u=Now%MOD;
for(int i=Laxt[u];i;i=Next[i]){
if(V[i]==Now) return true;
}
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
V[cnt]=Now;
return false;
}
int ans=;void _hash(int x,int y){
ULL Now=has[y]-has[x-]*sqr[y-x+];
if(!_insert(Now)) ++ans;
}
void _malacher()
{
int R=,Mid=,Len;
char c[maxn];
scanf("%s",c+);
Len=strlen(c+);
sqr[]=;
for(int i=;i<=Len;i++){
sqr[i]=sqr[i-]*P;
has[i]=has[i-]*P+c[i];
}
int r[maxn];
for(int i=;i<=Len;++i) {
_hash(i,i);
if(R>i) r[i]=min(r[*Mid-i],R-i);
while(i+r[i]+<=Len&&c[i+r[i]+]==c[i-r[i]-]){
_hash(i-r[i]-,i+r[i]+);
r[i]++;
}
if(i+r[i]>R) {
R=i+r[i];
Mid=i;
}
} cnt=;Mid=;R=;
memset(Laxt,,sizeof(Laxt));
memset(r,,sizeof(r));
for(int i=;i<=Len;++i) {
if(R>i) r[i]=min(r[*Mid-i],R-i+);
while(i+r[i]<=Len&&c[i+r[i]]==c[i-r[i]-]) {
_hash(i-r[i]-,i+r[i]);
++r[i];
}
if(i+r[i]->R) {
R=i+r[i]-;
Mid=i;
}
}
printf("%d\n",ans);
}
int main()
{
_malacher();
return ;
}
hiho1602本质不同的回文子串的数量的更多相关文章
- Hihocoder #1602 : 本质不同的回文子串的数量 manacher + BKDRhash
#1602 : 本质不同的回文子串的数量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串S,请统计S的所有子串中,有多少个本质不同的回文字符串? 注意如果 ...
- #1589 : 回文子串的数量(Manacher)
#1589 : 回文子串的数量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串S,请统计S的所有|S| * (|S| + 1) / 2个子串中(首尾位置不 ...
- 【HIHOCODER 1589】回文子串的数量(Manacher)
描述 给定一个字符串S,请统计S的所有|S| * (|S| + 1) / 2个子串中(首尾位置不同就算作不同的子串),有多少个是回文字符串? 输入 一个只包含小写字母的字符串S. 对于30%的数据,S ...
- WHU 583 Palindrome ( 回文自动机 && 本质不同的回文串的个数 )
题目链接 题意 : 给你一个串.要你将其划分成两个串.使得左边的串的本质不同回文子串的个数是右边串的两倍.对于每一个这样子的划分.其对答案的贡献就是左边串的长度.现在要你找出所有这样子的划分.并将贡献 ...
- URAL 2037 Richness of binary words (回文子串,找规律)
Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 最长回文子串(Longest Palindromic Substring)
这算是一道经典的题目了,最长回文子串问题是在一个字符串中求得满足回文子串条件的最长的那一个.常见的解题方法有三种: (1)暴力枚举法,以每个元素为中心同时向左和向右出发,复杂度O(n^2): (2)动 ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
随机推荐
- ScrambleString, 爬行字符串,动态规划
问题描述: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty su ...
- php调用mysql存储过程游标
<?php $dbtype = 'mysql'; $host = 'localhost'; $dbname = 'test'; $dsn = "$dbtype:host=$host;d ...
- [转载]在sublime中运行Java代码
1.设置java的PATH环境变量 2.创建批处理或Shell脚本文件 runJava.bat: 将该文件复制到JDK的bin目录下. @echo off cd %~dp1 echo Compilin ...
- CA证书,https讲解
关于具体连接过程,https://blog.csdn.net/wangjun5159/article/details/51510594 这篇博客写的应该比较准确. 我的理解,其中关键的一点是 http ...
- SQL Server死锁总结
1. 死锁原理 根据操作系统中的定义:死锁是指在一组进程中的各个进程均占有不会释放的资源,但因互相申请被其他进程所站用不会释放的资源而处于的一种永久等待状态. 死锁的四个必要条件:互斥条件(Mutua ...
- python 函数、模块、包及import导入方法
https://www.cnblogs.com/lijunjiang2015/p/7812996.html
- 搞懂分布式技术5:Zookeeper的配置与集群管理实战
搞懂分布式技术5:Zookeeper的配置与集群管理实战 4.1 配置文件 ZooKeeper安装好之后,在安装目录的conf文件夹下可以找到一个名为“zoo_sample.cfg”的文件,是ZooK ...
- linux---进程,(rpm,yum)软件包
3) 为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区,大小为5G fdisk -l fdisk /dev/sdb p 查看 n 新建 p 主分区 ...
- ansible入门四(Ansible playbook基础组件介绍)
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
- fegin--调用超时设置
Feign请求超时问题 Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码.而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间 ...