题意:

找出所有【i,j】为回文串【j+1,k】也为回文串的i*k乘积之和。

题解:

设sum1【i】 为正着插入,到 i 的所有回文串的起始位置的前缀和,sum2【i】 表示反正插入的前缀和

ans+=sum1【i]*sum1【i+1】

上面的式子很容易让我们想到两遍回文树正着和反着插入操作,

回文树的num【】表示到达 i 这个节点的回文串个数

我们用一个sum【i】数组到 i 的时候所有出现的回文串的长度的前缀和

sum1[i] = (1LL * (i + 1) * pam.num[pam.last] % mod - pam.sum[pam.last] + mod) % mod;

由于卡内存 ,所以我们就不记录sum2[]了

 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false)
#pragma comment(linker, "/STACK:102400000,102400000") using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
char s[maxn];
int sum1[maxn]; struct Palindrome_Automaton {
int len[maxn], next[maxn][], fail[maxn], sum[maxn];
int num[maxn], S[maxn], sz, n, last; int newnode(int l) {
for (int i = ; i < ; ++i)next[sz][i] = ;
num[sz] = , len[sz] = l;
return sz++;
} void init() {
sz = n = last = ;
newnode();
newnode(-);
S[] = -;
fail[] = ;
} int get_fail(int x) {
while (S[n - len[x] - ] != S[n])x = fail[x];
return x;
} void add(int c) {
c -= 'a';
S[++n] = c;
int cur = get_fail(last);
if (!next[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = next[get_fail(fail[cur])][c];
next[cur][c] = now;
num[now] = num[fail[now]] + ;
sum[now] = (sum[fail[now]] + len[now]) % mod;
}
last = next[cur][c];
} } pam; int main() {
//FIN;
while (~sfs(s+)) {
int n = strlen(s + );
pam.init();
for (int i = ; i<=n; ++i) {
pam.add(s[i]);
sum1[i] = (1LL * (i + ) * pam.num[pam.last] % mod - pam.sum[pam.last] + mod) % mod;
}
LL ans = ;
pam.init();
for (int i = n; i >= ; i--) {
pam.add(s[i]);
ans = (ans + sum1[i - ] * (1LL * (i - ) * pam.num[pam.last] % mod + pam.sum[pam.last]) % mod) % mod;
}
printf("%lld\n", ans);
}
return ;
}
 

Interesting HDU - 5785 回文树的更多相关文章

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

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

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

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

  3. HDU - 5421:Victor and String (回文树,支持首尾插入新字符)

    Sample Input 6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4 Sample Output 4 5 4 5 11 题意:多组输入,开始字符 ...

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

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

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

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

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

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

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

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

  8. Victor and String HDU - 5421 双向回文树

    题意: 有n种操作,开始给你一个空串,给你4中操作 1 c  在字符串的首部添加字符c 2 c  在字符串的尾部添加字符c 3  询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...

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

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

随机推荐

  1. jquery中的ajax请求用法以及参数详情

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  2. Android Telephony分析(七) ---- 接口扩展(异步转同步)

    本文是基于上一篇<Android Telephony分析(六) —- 接口扩展(实践篇)>来写的.上一篇介绍的接口扩展的方法需要实现两部分代码:1. 从APP至RIL,发送请求:2. 从R ...

  3. Feign 系列(05)Spring Cloud OpenFeign 源码解析

    Feign 系列(05)Spring Cloud OpenFeign 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/ ...

  4. 自从阿里买了Flink母公司以后,你不懂Flink就out了!

    个免费报名权限 Ps:小助理手动给大家发送资料,精力有限,仅限前100名免费领取,这份资料对于想要提升大数据技能进阶的小伙伴来说,将会是一份不可或缺的宝贵资料. 特别感谢飞总的部分原创支持!

  5. 5、如何快速找到多个字典中的公共键(key) 6 如何让字典保持有序 7 如何实现用户的历史记录功能(最多n条)

    5.如何快速找到多个字典中的公共键(key) from random import randint,sample #随机取数 # a = sample("ABCDEF",randi ...

  6. 在 Keil uVision4 MDK下配置开发STM32F103Z完整教程

    转载的,请原作者勿怪,以下为原链接: http://www.51hei.com/bbs/dpj-30359-1.html(欢迎直接查看原作者) 环境搭建: 1.安装 Keil uVision4 MDK ...

  7. Opencv 特征提取与检测-图像特征描述

    图像特征描述 什么是图像特征 可以表达图像中对象的主要信息.并且以此为依据可以从其它未知图像中检测出相似或者相同对象 常见的图像特征 常见的图像特征  边缘  角点  纹理 图像特征描述  描 ...

  8. pandas--层次化索引

    层次化索引是pandas的一项重要功能,它使你能在一个轴上拥有多个(两个以上)索引级别. 创建一个Series,并用一个由列表或数组组成的列表作为索引. data=Series(np.random.r ...

  9. Tools: windbg 使用指南

    windbg使用 符号表C:\Symbols; SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols 系统变量_NT_SYMBOL_PAT ...

  10. 记一次Mysql占用内存过高的优化过程

    一.环境说明: 操作系统:CentOS 6.5 x86_64 数据库:Mysql 5.6.22 服务器:阿里云VPS,32G Mem,0 swap 二.问题情况: 1.某日发现公司线上系统的Mysql ...