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 ...
随机推荐
- [洛谷P1231] 教辅的组成
题目大意:有n1本书,n2本练习册和n3个答案,然后又一些条件,说明某本答案可能和某本书对应,某本练习册可能和某本书对应,求最多有多少本完整的书(有书,练习册,答案) 题解:网络流,对应就连边,然后考 ...
- 算法学习——kruskal重构树
kruskal重构树是一个比较冷门的数据结构. 其实可以看做一种最小生成树的表现形式. 在普通的kruskal中,如果一条边连接了在2个不同集合中的点的话,我们将合并这2个点所在集合. 而在krusk ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- JavaScript的相等(==)与全等(===)
有段代码如下: view source print? 1 if (![] == []) { 2 //Code 3 } ![] == [],true or false? 我们都知道,ECMA ...
- [bzoj 2733]启发式合并权值线段树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 平衡树待学习.从一个博客学到了合并权值线段树的姿势:http://blog.csdn ...
- HDU3829:Cat VS Dog(最大独立集)
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- Python爬虫学习笔记之抓取猫眼的排行榜
代码: import json import requests from requests.exceptions import RequestException import re import ti ...
- (转)如何用python抓取网页并提取数据
最近一直在学这部分,今日发现一篇好文,虽然不详细,但是轮廓是出来了: 来自crifan:http://www.crifan.com/crawl_website_html_and_extract_inf ...
- es6+最佳入门实践(7)
7.set和map数据结构 7.1.什么是set? Set就是集合,集合是由一组无序且唯一的项组成,在es6中新增了set这种数据结构,有点类似于数组,但是它的元素是唯一的,没有重复 let st = ...
- httpFS访问
编辑文件httpfs-env.sh 执行sbin/httpfs.sh 执行命令curl -i "http://192.168.1.213:14000/webhdfs/v1?user.name ...