HDU - 5785:Interesting (回文树,求相邻双回文的乘积)
Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three tuple (i,j,k) satisfy 1≤i≤j<k≤length(S)
A palindrome string is a string that is same when the string is
read from left to right as when the string is read from right to left.
InputThe input contains multiple test cases.
Each test case contains one string. The length of string is between 1 and 1000000. String only contains lowercase letter.OutputFor each test case output the answer mod 1000000007.Sample Input
aaa
abc
Sample Output
14
8
题意:累计i*k的和,如果[i,j],[j+1,k]都是回文串;
思路:统计以j为结尾的回文串个数numj,以及他们的长度和addj; 以j+1为首....;那么这个位置的贡献就是(numj*(j+1)-addj)*(numj+1*j+addj+1);
此题要节约空间,所以不要开longlong的数组。
#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=;
const int Mod=1e9+;
struct PAT
{
struct node{
int len,num,fail,son[],add;
}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[]=-;
}
void 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[v].add=(t[t[v].fail].add+t[v].len)%Mod;
t[p].son[c]=v;
}
last=t[p].son[c];
}
}T;
int ans,sum[maxn];char c[maxn];
int main()
{
while(~scanf("%s",c+)){
int N=strlen(c+);
T.init(); ans=;
rep(i,,N) {
T.add(c[i]-'a');
sum[i]=(1LL*T.t[T.last].num*(i+)-T.t[T.last].add)%Mod;
}
T.init();
rep2(i,N,){
T.add(c[i]-'a');
ans+=(ll)(T.t[T.last].add+1LL*T.t[T.last].num*(i-)%Mod)*sum[i-]%Mod;
ans%=Mod;
}
printf("%d\n",ans);
}
return ;
}
HDU - 5785:Interesting (回文树,求相邻双回文的乘积)的更多相关文章
- HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- hdu 3068 最长回文(manachar求最长回文子串)
题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...
- HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)
CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- HDU 5785 Interesting
题目: 大概说给一个字符串,找到其所有子串[i...k]满足它是由两个回文串拼成的,求Σi*k. 分析: 用val[1][i]表示以i结尾的回文串的起始位置的和val[0][i]表示以i起始的回文串的 ...
- HDU 5785 Interesting manacher + 延迟标记
题意:给你一个串,若里面有两个相邻的没有交集的回文串的话,设为S[i...j] 和 S[j+1...k],对答案的贡献是i*k,就是左端点的值乘上右端点的值. 首先,如果s[x1....j].s[x2 ...
- hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...
- HDU - 1542 Atlantis(线段树求面积并)
https://cn.vjudge.net/problem/HDU-1542 题意 求矩形的面积并 分析 点为浮点数,需要离散化处理. 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x ...
- 【BZOJ2160】拉拉队排练(回文树)
[BZOJ2160]拉拉队排练(回文树) 题面 BZOJ 题解 看着题目, 直接构建回文树 求出每个回文串的出现次数 直接按照长度\(sort\)一下就行了 然后快速幂算一下答案就出来了 这题貌似可以 ...
随机推荐
- python字典和列表的高级应用
1.将序列分解为单独的变量 1.1问题 包含n个元素的元组或列表.字符串.文件.迭代器.生成器,将它分解为n个变量 1.2方案 直接通过赋值操作 要求:变量个数要等于元素个数 当执行分解操作时,有时需 ...
- Linux第八周作业
一 理解编译链接的过程和ELF可执行文件格式 这张图说明了可执行程序的产生 大致过程为 .c文件汇编成汇编代码.asm, 然后再汇编成目标码.o, 然后链接成可执行文件a.out, 这时可执行文件就可 ...
- Vue + Element UI 实现权限管理系统(第三方图标库)
使用第三方图标库 用过Elment的同鞋都知道,Element UI提供的字体图符少之又少,实在是不够用啊,幸好现在有不少丰富的第三方图标库可用,引入也不会很麻烦. Font Awesome Font ...
- xadmin后台导出时gunicorn报错ascii
django + xadmin + nginx + gunicorn部署后,xadmin后台导出model数据报错,gunicorn日志记录为:UnicodeEncodeError: 'ascii' ...
- unity中让物体移动到鼠标点击地面任一点的位置(单击移动和双击暂停移动)并生成图标
using UnityEngine; using System.Collections.Generic; using UnityEngine.EventSystems; using UnityEngi ...
- 用老毛桃U盘安装:[3]Ghost版Win7系统
用老毛桃自动安装Ghost版Win7的步骤: 1,到网上先下载Ghost版Win7映像文件到硬盘,我放到的是U盘,盘符为Z,如果你愿意,可直接放到硬盘即可,放到硬盘安装速度会快一点. 2,把制作好的老 ...
- java局部变量,成员变量在堆和栈中的存储
对于局部变量,如果是基本类型,会把值直接存储在栈:如果是引用类型,比如String s = new String("william");会把其对象存储在堆,而把这个对象的引用(指针 ...
- nginx启用php
1. php下载https://secure.php.net/downloads.php搜索china镜像站点,从这里下载http://cn2.php.net/get/php-7.2.3.tar.gz ...
- java关于集合的遍历与增强for循环(foreach)的使用
java集合类的使用可以说是无处不在,总的我们可以将之分为三大块,分别是从Collection接口延伸出的List.Set和以键值对形式作存储的Map类型集合. 许多情况需要我们遍历出集合 ...
- java 2D图形绘制
package jisuan; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; imp ...