题意翻译

求一个串中包含几个回文串

题目描述

Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "malayalam" can be created by some ways:

  • malayalam = m + ala + y + ala + m
  • malayalam = m + a + l + aya + l + a + m

We want to take the value of function NumPal(s) which is the number of different palindromes that can be created using the string S by the above method. If the same palindrome occurs more than once then all of them should be counted separately.

输入输出格式

输入格式:

The string S.

输出格式:

The value of function NumPal(s).

输入输出样例

输入样例#1: 复制

malayalam

输出样例#1: 复制

15

说明

Limitations

0 < |s| <= 1000


题解

这不是一道傻逼模板题吗?

我们只需要好好利用一下cnt数组,统计一下len不为1的回文串。

然后再加上一个|s|的长度就好了。


代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
int fail,len,ch[26],cnt;
}t[1000010];
char s[1000100];
int n,ans;
void solve()
{
int k=0,tot=1;t[0].fail=t[1].fail=1;t[1].len=-1;s[0]='$';
for(int i=1;i<=n;i++)
{
while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
if(!t[k].ch[s[i]-'a']){
t[++tot].len=t[k].len+2;
int j=t[k].fail;
while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
t[tot].fail=t[j].ch[s[i]-'a'];
t[k].ch[s[i]-'a']=tot;
}
k=t[k].ch[s[i]-'a'];
t[k].cnt++;
}
for(int i=tot;i>=2;i--)
t[t[i].fail].cnt+=t[i].cnt;
for(int i=tot;i>=2;i--)
if(t[i].len!=1)
ans+=t[i].cnt;
} int main()
{
scanf("%s",s+1);
n=strlen(s+1);
solve();
cout<<ans+n<<endl;
}

SP7586 NUMOFPAL - Number of Palindromes(回文树)的更多相关文章

  1. SPOJ Number of Palindromes(回文树)

    Number of Palindromes Time Limit: 100MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu ...

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

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

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

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

  4. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  5. CF245H Queries for Number of Palindromes(回文树)

    题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...

  6. URAL 2040 Palindromes and Super Abilities 2(回文树)

    Palindromes and Super Abilities 2 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d ...

  7. 【Aizu2292】Common Palindromes(回文树)

    [Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...

  8. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

  9. Gym - 101806Q:QueryreuQ(回文树)

    A string is palindrome, if the string reads the same backward and forward. For example, strings like ...

随机推荐

  1. sql server 中文乱码

    在数据库中查询每个字段的备注信息(备注信息是用中文写的),查询结果却是乱码,如图: 百度说需要设置数据库的排序规则,设置成中文的,结果还是报5030错误,无法修改字符集为Chinese_PRC_CI_ ...

  2. 当一个元素被浮动后,它的display是否会被默认指定为block?

    css 浮动后的元素不论是什么display的都默认是block就是设置inline也是block IE/6出现双边框的原因 出现双边距的条件是当浮动元素的浮动方向和margin的方向一致时才会出现. ...

  3. 玩转HTML5移动页面(动效篇)

    为一名前端,在拿到设计稿时你有两种选择: 快速输出静态页面 加上高级大气上档次狂拽炫酷屌炸天的动画让页面动起来 作为一个有志向的前端,当然是选2啦!可是需求时间又很短很短,怎么办呢? 这次就来谈谈一些 ...

  4. VUE项目axios请求头更改Content-Type

    const httpServer = (opts, data) => { const token = localStorage.getItem('token') const PUBLIC = ` ...

  5. Solr全文检索引擎配置及使用方法

    介绍 Solr是一款开源的全文检索引擎,基于lucene.拥有完善的可配置功能界面.具有丰富的查询语言,可扩展,可优化. 下载安装 进入solr官网下载包(这里我使用的版本是8.0) http://w ...

  6. Python+Appium来写app自动化脚本

    1...........................我有空再补

  7. 常用的pdf工具

    https://www.ilovepdf.com/zh-cn https://smallpdf.com/cn/compress-pdf https://www.pdf2go.com/zh/compre ...

  8. C++容器(二):关联容器简介

    关联容器(associative container)与顺序容器的本质区别在于:关联容器通过键(Key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素.虽然,关联容器的大部分行为 ...

  9. Qt之QStackedWidget

    简述 QStackedWidget继承自QFrame. QStackedWidget类提供了多页面切换的布局,一次只能看到一个界面. QStackedWidget可用于创建类似于QTabWidget提 ...

  10. 集团公司(嵌入ETL工具)財务报表系统解决方式

    集团公司(嵌入ETL工具)財务报表系统解决方式 一.项目背景: 某集团公司是一家拥有100多家子公司的大型集团公司,旗下子公司涉及各行各业,包含:金矿.铜矿.房产.化纤等.因为子公司在业务上的差异.子 ...