HDU 5157 Harry and magic string(回文树)
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
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.
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].
aca
aaaa
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(回文树)的更多相关文章
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- HDU.5394.Trie in Tina Town(回文树)
题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...
- HDU 5658 CA Loves Palindromic(回文树)
CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/O ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
- 2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...
- hdu5421 Victor and String 回文树(前后插入)
题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...
- 回文树练习 Part1
URAL - 1960 Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...
- HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
随机推荐
- ASP.NET前后台交互之JSON数据
最近由于项目需要做一个Ajax的搜集单表单的多重数据的需求,所以就采用了前端JQuery保存Object对象之后转换成JSON的数据源传递给后台处理的这样的形式,相信有不少人大多时候是接收后台给出的J ...
- 【搞机】Apple Pencil 开箱
前言 上次入手了新的iPad Pro .好开心呢. 然后发现官方的笔不错呢~ 后来,苹果官方的12期免息分期又回来啦~ 买买买!!! 上图 体验 官方的笔真的不愧叫Pencil ,完美模拟铅笔的手感. ...
- 2012全球SEO行业调查报告
这份报告是SEOmoz对每两年一度举办的SEO行业调查进行的分析数据,上次调查是在2010年.该调查,主要围绕SEO从业人员的特征.工作内容时间分配比例.SEO相关消费和预算.对未来市场的看法.seo ...
- .net 非阻塞事件获取返回异步回调结果
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 消息成功失败回调demo
) )); try {// ListenableFuture<ResponseEntity<String>> future = restTemplate.postForEnti ...
- CSU 1329: 一行盒子
1329: 一行盒子 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 740 Solved: 145[Submit][Status][Web Board ...
- Oracle之标示符无效
一.引言 今天使用Oracle客户端执行一条sql语句 order by colname3 结果一直提示标示符无效,以为是自己把列名写错了打开表的列,一个字母一个字母的比对,还是没有错 二.原因及解决 ...
- 1.1 Application Fundamentals - 应用原理
Android应用是使用Java编程语言编写的.Android SDK工具把代码.资源和数据文件编译为一个Android包,这是一个有.apk后缀的压缩文件.一个单独的.apk文件里包含所有的代码,这 ...
- GraphicsMagick +im4java高并发处理大型网站图片工具-图片剪切、遮蔽、水印添加之环境搭建
环境: centos 6.5 GraphicsMagick 下载安装 准备环镜: 需要依赖zlib图片操作函数库 下载地址:http://www.zlib.net/ 编译安装 .tar.gz cd z ...
- 总结几个关于 jQuery 用法
有关 jquery 用法 目录: $.trim() $.inArray() $.getJSON() 事件委托 on 遍历closest() ajaxSubmit() 拖拽排序 dragsort() 进 ...