题意翻译

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

题目描述

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. Swift学习笔记(9):枚举

    目录: 基本语法 关联值 原始值 枚举为一组相关的值定义了一个共同的类型. ・可以给枚举成员指定原始值类型:字符串,字符,整型值或浮点数等 ・枚举成员可以指定任意类型的关联值存储到枚举成员中 ・枚举可 ...

  2. C# 对象初始化器和集合初始化器

    /// <summary>/// 图书类/// </summary>public class Book {     /// <summary>     /// 图书 ...

  3. js产生随机数的几个方法

    1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分. 3.Math.round(n ...

  4. 查看锁表进程SQL语句

    查看锁表进程SQL语句   set pagesize 999 set line180 col ORACLE_USERNAME for a18 col OS_USER_NAME for a18 col ...

  5. eclipse的springMVC环境搭建并输出HelloWorld

    spring简单介绍:https://www.cnblogs.com/package-java/p/10368672.html 1.创建一个Maven Project项目 点击下一步 点击下一步 2. ...

  6. CentOS7-1810 系统DNS服务器BIND软件配置说明

    DNS的出现的历史 网络出现的早期是使用IP地址通讯的,那时就几台主机通讯.但是随着接入网络主机的增多,这种数字标识的地址非常不便于记忆,UNIX上就出现了建立一个叫做hosts的文件(Linux和W ...

  7. Linux CentOs6.5误卸载自带python和yum后的解决办法

    事故背景:前几天因项目需要,在服务器上搭建python-mysql模块,结果没安装好,于是乎想卸载重装,遂在网上查询卸载python的方法,结果一不小心直接把系统的python删了个干净....... ...

  8. [React Native] Dismiss the Keyboard in React Native

    In this lesson you will learn how create a re-usable component that gives the user the ability to di ...

  9. (六)storm-kafka源代码走读之PartitionManager

    PartitionManager算是storm-kafka的核心类了,如今開始简单分析一下.还是先声明一下,metric部分这里不做分析. PartitionManager主要负责的是消息的发送.容错 ...

  10. V$ASM_OPERATION