Sample Input

aca
aaaa

Sample Output

3
15

题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串。

思路:可以用回文树求出以每个位置结尾的回文串数,那么累加得到前缀和; 倒着再做一遍得到每个位置为开头的回文串数,乘正向求出的前缀和即可。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=;
struct PAT
{
struct node{
int len,num,fail,son[];
}t[maxn];
int last,n,tot,s[maxn];
void init()
{
memset(t,,sizeof(t));
tot=last=; n=;
t[].len=; t[].len=-;
t[].fail=t[].fail=;
s[]=-;
}
int add(int c){
int p=last; s[++n]=c;
while(s[n]!=s[n--t[p].len]) p=t[p].fail;
if(!t[p].son[c]){
int v=++tot,k=t[p].fail;
while(s[n]!=s[n-t[k].len-]) k=t[k].fail;
t[v].fail=t[k].son[c];
t[v].len=t[p].len+;
t[v].num=t[t[v].fail].num+;
t[p].son[c]=v;
}
last=t[p].son[c];
return t[last].num;
}
}T;
ll ans,sum[maxn];char c[maxn];
int main()
{
while(~scanf("%s",c+)){
int N=strlen(c+);
T.init(); ans=;
rep(i,,N) sum[i]=sum[i-]+T.add(c[i]-'a');
T.init();
rep2(i,N,) ans+=sum[i-]*T.add(c[i]-'a');
printf("%lld\n",ans);
}
return ;
}

HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)的更多相关文章

  1. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)

    CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...

  3. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  4. 回文树练习 Part1

    URAL - 1960   Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...

  5. I Love Palindrome String HDU - 6599 回文树+hash

    题意: 输出每个长度下的回文串(这些回文串的左半边也需要是回文串) 题解: 直接套上回文树,然后每找到一个回文串就将他hash. 因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半 ...

  6. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

  7. 回文树&后缀自动机&后缀数组

    KMP,扩展KMP和Manacher就不写了,感觉没多大意思.   之前感觉后缀自动机简直可以解决一切,所以不怎么写后缀数组.   马拉车主要是通过对称中心解决问题,有的时候要通过回文串的边界解决问题 ...

  8. 回文树 Palindromic Tree

    回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...

  9. CodeForces 17E Palisection(回文树)

    E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...

随机推荐

  1. git Please move or remove them before you can merge.

    git clean -d -fx "" 其中  x -----删除忽略文件已经对git来说不识别的文件 d -----删除未被添加到git的路径中的文件 f -----强制运行

  2. centos6.5 安装PHP7.0支持nginx

    1.安装PHP所需要的扩展           yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel ...

  3. python 学习 map /reduce

    python 内建了map()和reduce()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. ...

  4. day037 行记录的操作

    1.库操作 2.表操作 3.行操作 1.库操作 1)创建数据库 语法: create database 数据库名 charset utf8; 数据库命名规则: 由数字,字母,下划线,@,#,$ 等组成 ...

  5. node连接myslq

    var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : ' ...

  6. 打开和写入word文档

    一. 使用win32读取word内容 # -*- coding: utf-8 -*- from win32com import client as wc def readDocx2(): word = ...

  7. 7 Serial Configuration 理解(三)

    *Dynamic Reconfiguration Port(DRP) 动态重配置端口:在7系列FPGA中,配置存储器主要用于实现用户逻辑,连接和I / O,但它也用于其他目的. 例如,它用于指定功能块 ...

  8. 安装连接mysql8时候遇到的问题以及解决(转)

    官网下载mysql8的安装包: https://dev.mysql.com/downloads/ 下一步安装即可. mysql8增加了传说中的安全性校验 遇到的几个问题: 1.natcat连接不上.参 ...

  9. 运行时常量池中的符号引用/String.intern() /ldc指令

    运行时常量池,之前放在方法区(永久代)中,1.8之后被转移到元空间,放到了native memory中. 具体的数据结构是:(看对象的内存布局,句柄访问还是对象头中保存指向类的元数据的指针,这里以对象 ...

  10. day 54 jQuery 的初步基础

    jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交互, ...