Harry and magic string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 223    Accepted Submission(s): 110

Problem Description
Harry got a string T, he wanted to know the number of T’s disjoint palindrome substring pairs. A string is considered to be palindrome if and only if it reads the same backward or forward. For two substrings of T:x=T[a1…b1],y=T[a2…b2](where
a1 is the beginning index of x,b1 is
the ending index of x. a2,b2 as
the same of y), if both x and y are palindromes and b1<a2 or b2<a1 then
we consider (x, y) to be a disjoint palindrome substring pair of T.
 
Input
There are several cases.

For each test case, there is a string T in the first line, which is composed by lowercase characters. The length of T is in the range of [1,100000].
 
Output
For each test case, output one number in a line, indecates the answer.
 
Sample Input
aca
aaaa
 
Sample Output
3
15
求一个字符串中所有不相交的回文串对
回文树,先倒着扫一遍,再正着扫一遍
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
const int MAX=1e5+5;
char str[MAX];
struct Tree
{
int next[MAX][26];
int fail[MAX];
LL num[MAX];
int len[MAX];
int s[MAX];
int last;
int n;
int p;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
num[p]=0;
len[p]=x;
return p++;
}
void init()
{
p=0;
new_node(0);
new_node(-1);
last=0;
n=0;
s[0]=-1;
fail[0]=1;
}
int get_fail(int x)
{
while(s[n-len[x]-1]!=s[n])
x=fail[x];
return x;
}
LL add(int x)
{
x-='a';
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
next[cur][x]=now;
num[now]=num[fail[now]]+1;
last=now;
}
return num[last];
}
}tree;
LL sum[MAX];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
sum[len]=0;
tree.init();
for(int i=len-1;i>=0;i--)
{
sum[i]=sum[i+1]+tree.add(str[i]);
}
tree.init();
LL ans=0;
for(int i=0;i<len;i++)
{
ans+=(LL)tree.add(str[i])*sum[i+1];
}
printf("%lld\n",ans);
}
return 0;
}

HDU 5157 Harry and magic string(回文树)的更多相关文章

  1. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  2. HDU.5394.Trie in Tina Town(回文树)

    题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...

  3. HDU 5658 CA Loves Palindromic(回文树)

    CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/O ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

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

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

  6. hdu5421 Victor and String 回文树(前后插入)

    题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...

  7. 回文树练习 Part1

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

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

    Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...

  9. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

随机推荐

  1. html调用摄像头的方法汇总

    1.在PC端打开摄像头的方法:(移动端不能使用) 能够实现打开摄像头并截图 <!doctype html> <html lang="en"> <hea ...

  2. 【SSH之旅】一步步学习Struts1框架(二):Struts实例

    从上篇博客能够看到,事实上Struts1框架就是封装了一些页面的转向.数据类型的转换,去除冗余的if else推断.除了这些,事实上还封装了一些我们寻经常使用的JSTL标签库,文件上传等等. 以下看怎 ...

  3. 深度学习之Matlab 转C++在iOS上測试CNN手型识别

    1 前言 在上一篇Blog.我介绍了在iOS上执行CNN的一些方法. 可是,一般来说.我们须要一个性能强劲的机器来跑CNN,我们仅仅只是须要将得到的结果用于移动端. 之前在Matlab使用UFLDL的 ...

  4. 状压dp Mondriaan&#39;s Dream poj2411

    超经典的一道题目,实现这题的方法也有非常多种 1.利用DFS建立矩阵,然后通过高速矩阵幂得到答案(运用于min(m,n)比較小.可是max(m,n)很大的情况) 2.利用dp状压解决 第一种在我的还有 ...

  5. php实现IP地址和数字相互转换

    echo $a=ip2long ("202.97.224.68");//地址转换成数字 系统自带的函数 注:这里面有一个要注意的地方,大概由于PHP无法正确判断转换完的数字类型,出 ...

  6. PhpStorm和PHPstudy配置调试参数(Xdebug),问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.

    配置phpstrom的Xdebug 问题描述: Error. Interpreter is not specified or invalid. Press "Fix" to edi ...

  7. Laravel建站03--建立前台文章列表和文章详情

    经过了前面的开发环境搭建和数据库配置.git配置的准备工作以后,现在可以开始动作做点什么了. 先从简单的开始,那就先来个文章列表和详情页吧. 这期间可以学习到路由.建立model.controller ...

  8. MySQL索引长度限制问题

    在修改表结构时出现了错误:Specified key was too long;max key length is 1000 bytes. MySQL版本为Server version: 5.1.36 ...

  9. Java并发编程(三):并发模拟(工具和Java代码介绍)

    并发模拟工具介绍 ① Postman : Http请求模拟工具 从图上我们可以看出,Postman模拟并发其实是分两步进行操作的.第一步:左边的窗口,在窗口中设置相关接口以及参数,点击运行进行第二步. ...

  10. position固定菜单栏

    position:fixed来实现. <!DOCTYPE html> <html lang="en"> <head> <meta char ...