Manacher【SP7586】NUMOFPAL - Number of Palindromes
Description
求一个串中包含几个回文串。
Input
输入一个字符串\(S\)
Output
包含的回文串的个数.
看到题解里面有人人写回文自动机.
有必要那么麻烦嘛 emmm
我们直接跑\(Manacher\)就好了啊.
答案就是以每一位为中心的回文串长度/2的和。
(如果添加字符则为回文半径长度/2。)
这个就不多解释了.很明显的一个东西。
不能理解的话,可以看下这个
# a # a # b # a # a #
1 2 3 2 1 6 1 2 3 2 1
数字对应于每一个位置的回文半径
.(实际上减去\(1\)才是,但是计算的时候要加上1,所以代码里面直接用了这个.)
代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define R register
using namespace std;
const int maxn=1008;
char ch[maxn];
char s[maxn<<2];
int len,RL[maxn<<2],MaxRight,center,ans;
int main()
{
scanf("%s",ch);
len=strlen(ch);
for(R int i=0;i<len;i++)s[2*i+1]=ch[i];
len=2*len+1;
for(R int i=0;i<len;i++)
{
if(i<=MaxRight)
RL[i]=min(RL[2*center-i],MaxRight-i);
else RL[i]=1;
while(s[i-RL[i]]==s[i+RL[i]] and i-RL[i]>=0 and i+RL[i]<len)
RL[i]++;
if(i+RL[i]-1>MaxRight)
MaxRight=i+RL[i]-1,center=i;
}
for(R int i=0;i<len;i++)ans+=RL[i]/2;
printf("%d",ans);
}
Manacher【SP7586】NUMOFPAL - Number of Palindromes的更多相关文章
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)
The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...
- SP7586 NUMOFPAL - Number of Palindromes 解题报告
SP7586 NUMOFPAL - Number of Palindromes 题意翻译 求一个串中包含几个回文串 输入输出格式 输入格式: The string S. 输出格式: The value ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
随机推荐
- 在iis上部署ssl证书 https
1.取走证书下载下来的文件.解压iis的压缩包. 2.打开internet信息服务iis管理器 3.双击打开后,选择导入,导入我们刚刚解压得到的pfx文件,这个pfx文件就是你需要部署域名的那个文件. ...
- 深入研究JavaScript的事件机制
本篇开始将回顾下Javascript的事件机制.同时会从一个最小的函数开始写到最后一个具有完整功能的,强大的事件模块.为叙述方便将响应函数/回调函数/事件Listener/事件handler都称为事件 ...
- Codeforces Round #350 (Div. 2) A
A. Holidays time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- POJ 3104 Drying(二分
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22163 Accepted: 5611 Descripti ...
- wget、yum、rpm、apt-get区别
wget 类似于迅雷,是一种下载工具, 通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理 名字是World Wide Web”与“get”的结合. yum: 是 ...
- Math.abs为Integer.Min_VALUE返回错误的值
Math.abs为Integer.Min_VALUE返回错误的值 这段代码: System.out.println(Math.abs(Integer.MIN_VALUE)); 回报-2147483 ...
- CentOS 安装 debuginfo-install
安装debuginfo相关的包步骤如下: 1. 修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo中的enabled参数,将其值修改为1 2. 使用命令: yum i ...
- x:Class, x:Key
x:Class: 用来创建一个partial的class, 比如默认生成的x:Class="MyTest.MainWindow", 会自动生成一个MainWindow的partia ...
- WebKit学习资源
1.http://blog.csdn.net/dlmu2001/article/category/741748 红心地瓜 2.http://blog.csdn.net/cnnzp/article ...
- POJ 1064---二分搜索法
///2.假定一个解并判断是否可行 ///POJ1064 /** Q:有N条绳子,长度分别为Li,从中切割出k条长度相同的绳子, 这K条绳子最长能有多长?保留两位小数 A: 二分搜索模型. 条件C(x ...