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的更多相关文章

  1. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  2. 【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 ...

  3. SP7586 NUMOFPAL - Number of Palindromes 解题报告

    SP7586 NUMOFPAL - Number of Palindromes 题意翻译 求一个串中包含几个回文串 输入输出格式 输入格式: The string S. 输出格式: The value ...

  4. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  8. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  9. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

随机推荐

  1. 【题解】HAOI2012高速公路

    一节政治课的结果……推式子+推式子+推式子…… 首先注意到一个区间里面,选择(x, y)和(y, x)的费用是一样的.所以我们把这两种情况合为一种,那么现在询问的区间为(l, r),则一共的情况就有 ...

  2. [NOIP2017 TG D2T2]宝藏

    题目大意:给定一个有重边,边有权值的无向图.从某一个点出发,求到达所有的点需要的最少费用,并且限制两点之间只有一条路径.费用的计算公式为:所有边的费用之和.而边$x->y$的费用就为:$y$到初 ...

  3. bootstrap和bootstrap-select去除蓝色边框outline

    /*bootstrap outline设置*/ .btn:focus, .btn:active:focus, .btn.active:focus, .btn.focus, .btn:active.fo ...

  4. spring中Constructor、@Autowired、@PostConstruct的顺序【转】

    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...

  5. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C (用map 超时)

    C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A

    A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. sublime2创建一个html5的snippets文件

    背景:跟了一个网上课程,老师哗啦啦敲代码,屏幕上只敲了几个字,键盘一操作,瞬间一大溜代码,看得我心惊肉跳连忙暂停抄抄抄. 举个简单的例子,我需要创建一个html文件.但是我不想每次都敲固定的格式.那么 ...

  8. Win7/8 绿色软件开机启动

    在查找番茄工作法PC端软件时,发现了淡高的文章win8绿色软件开机启动,试用了一下wintabs,的确好用! 另外,office软件中有一款 OFFICE tabs的插件,标签式的管理,非常方便快捷, ...

  9. autoKeras入门

    测试本地mnist数据集 图片只用500张,450张做train与50张test, 代码如下: # conding:utf-8 import os os.environ[' import numpy ...

  10. bzoj 2152 点剖分

    比较裸的点剖分,访问到每个重心时,记录一个b数组, 代表当前子树中长度为i的边的数量是b[i],因为是3的倍数, 所以边长mod 3保存就行了,然后记录一个sum数组,代表 当前子树中一个子节点为根的 ...