Interesting HDU - 5785 回文树
题意:
找出所有【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 回文树的更多相关文章
- I Love Palindrome String HDU - 6599 回文树+hash
题意: 输出每个长度下的回文串(这些回文串的左半边也需要是回文串) 题解: 直接套上回文树,然后每找到一个回文串就将他hash. 因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半 ...
- HDU.5394.Trie in Tina Town(回文树)
题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...
- 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 题意:多组输入,开始字符 ...
- 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 ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 5658 CA Loves Palindromic(回文树)
CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/O ...
- Victor and String HDU - 5421 双向回文树
题意: 有n种操作,开始给你一个空串,给你4中操作 1 c 在字符串的首部添加字符c 2 c 在字符串的尾部添加字符c 3 询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
随机推荐
- eclipse创建maven项目时报错解决办法
1.Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1.1 from any of ...
- Linux内存管理(深入理解Linux内核)
Linux的内存管理,实际上是借助80x86的硬件分段和分页电路,将逻辑地址转化为物理地址的. 物理内存中,有一部分是一直(Permanently)映射给内核使用的,这部分主要用于保存内核的代码,以及 ...
- JAVA FileUtils(文件读写以及操作工具类)
文件操作常用功能: package com.suning.yypt.business.report; import java.io.*; import java.util.*; @SuppressWa ...
- PAT_A1103#Integer Factorization
Source: PAT A1103 Integer Factorization (30 分) Description: The K−P factorization of a positive inte ...
- java中trim()方法是用来干什么的?
trim()的作用是去掉字符串两端的多余的空格,注意,是两端的空格,且无论两端的空格有多少个都会去掉,当然 中间的那些空格不会被去掉,如: String s = " a s f g ...
- java IO 类概述表
列举常用的类方便查看,温故知新! byte input byte output character input character output Basic InputStream OutputStr ...
- 并发编程(五)——GIL全局解释器锁、死锁现象与递归锁、信号量、Event事件、线程queue
GIL.死锁现象与递归锁.信号量.Event事件.线程queue 一.GIL全局解释器锁 1.什么是全局解释器锁 GIL本质就是一把互斥锁,相当于执行权限,每个进程内都会存在一把GIL,同一进程内的多 ...
- jenkins构建参数
$BUILD_NUMBER 构建序号 $WORKSPACE 工作目录 #!/bin/sh - chmod u+x mvnw./mvnw package -Pprod -Dmaven.test.skip ...
- linux 服务器修改密码
登录服务器后直接输入命令行passwd root 然后输入两次新密码就行
- git 强制取消本地修改
本地的项目中修改不做保存操作,可以用到Git pull的强制覆盖,具体代码如下: git fetch --allgit reset --hard origin/master git fetch 指令是 ...