Codeforces.GYM100548G.The Problem to Slow Down You(回文树)
\(Description\)
给定两个串\(S,T\),求两个串有多少对相同回文子串。
\(|S|,|T|\leq 2\times 10^5\)。
\(Solution\)
好菜啊QAQ 这都没想到
对两个串分别建回文树,两个串有相同的回文串当且仅当存在相同的节点。
所以分别从两棵树的两个根(\(0\)和\(1\))DFS,只走两棵树相同的节点,把经过节点的贡献加上就行了。
不要求本质不同,所以要更新一次val[fail[x]]。
把两个串用'$'接成一个串,直接建\(PAM\),分别求两边的\(val\)也可以,本质是一样的。比如这个。
//202ms 51800KB
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
const int N=2e5+5;
struct PAM
{
int tot,las,son[N][26],len[N],val[N],fail[N];
char s[N];
inline void Init()
{
las=tot=1, fail[0]=1, len[1]=-1, s[0]='$';
memset(son[0],0,sizeof son[0]), memset(son[1],0,sizeof son[1]);//只清空这两个就可以了啊
}
inline int Find(int x,int n)
{
while(s[n]!=s[n-len[x]-1]) x=fail[x];
return x;
}
void Insert(int c,int n)
{
int p=Find(las,n);
if(!son[p][c])
{
int np=++tot;
memset(son[np],0,sizeof son[np]), val[np]=0;
fail[np]=son[Find(fail[p],n)][c];
son[p][c]=np, len[np]=len[p]+2;
}
++val[las=son[p][c]];
}
void Build()
{
Init(), scanf("%s",s+1);
for(int i=1,l=strlen(s+1); i<=l; ++i) Insert(s[i]-'a',i);
for(int i=tot; i>1; --i) val[fail[i]]+=val[i];
val[0]=val[1]=0;
}
}S,T;
LL DFS(int x,int y)
{
LL res=1ll*S.val[x]*T.val[y];
for(int i=0; i<26; ++i)
if(S.son[x][i]&&T.son[y][i]) res+=DFS(S.son[x][i],T.son[y][i]);
return res;
}
int main()
{
int tot; scanf("%d",&tot);
for(int Cs=1; Cs<=tot; ++Cs)
S.Build(), T.Build(), printf("Case #%d: %I64d\n",Cs,DFS(0,0)+DFS(1,1));
return 0;
}
Codeforces.GYM100548G.The Problem to Slow Down You(回文树)的更多相关文章
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...
- Codeforces 932G Palindrome Partition - 回文树 - 动态规划
题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- Codeforces 932G Palindrome Partition 回文树+DP
题意:给定一个串,把串分为偶数段 假设分为$s_1,s_2,s_3....s_k$ 求满足$ s_1=s_k,s_2=s_{ k-1 }... $的方案数模$10^9+7$ $|S|\leq 10^6 ...
- Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...
- CodeForces 17E Palisection(回文树)
E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...
- Palisection(Codeforces Beta Round #17E+回文树)
题目链接 传送门 题意 给你一个串串,问你有多少对回文串相交. 思路 由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数. 而不相交的回文对数可以通过计算以\(i\)为右端点的回文 ...
随机推荐
- 创建Python虚拟环境
以window为例: 安装完python后, 打开cmd, 命令行输入: pip install virtualenv ,安装过程见截图 进入你想安装虚拟环境的目录, 命令行输入: virtualen ...
- excel vba获取拼音
Function PinYin2(Hz As String) Dim PinMa As String Dim MyPinMa As Variant Dim Temp As Integer, i As ...
- XML使用与总结
xml是一种比较方便的数据储存方式,它适用于小数据的存储.最常见的适用地方莫过于各种web.config与app.config了. 一.创建一个简单的xml路径 public static str ...
- MySQL 官方 Docker 镜像的使用
首先是pull image,这里我拉取的是5.6.35: $ sudo docker pull mysql:5.6.35 拉下来以后大可以按照官方的说明无脑启动,但是外部无法访问,所以绑定端口: $ ...
- Adjoint operators $T_K$ and $T_{K^{*}}$ in BEM
In our last article, we introduced four integral operators in the boundary integral equations in BEM ...
- Elasticsearch - Scroll
Scroll Version:6.1 英文原文地址:Scroll 当一个搜索请求返回单页结果时,可以使用 scroll API 检索体积大量(甚至全部)结果,这和在传统数据库中使用游标的方式非常相似. ...
- sendEmail 阿里云使用587端口
使用sendEmail使用参数 -o tls=yes -s smtp服务器+端口 因为阿里云屏蔽了25端口,我使用465也不成功,只有使用587端口,我使用的qq企业邮箱,测试可用
- LVM实现逻辑卷镜像
本文系统 CentOS 6.5 x64 LVM的镜像功能,有点儿类似于Raid1,即多块儿磁盘互相同步,确保资料不会丢失. 1.在此添加4块物理硬盘,每块2G空间 2.将sdb.sdc.sdd.sde ...
- Codeforces 1111D Destroy the Colony 退背包 (看题解)
第一次知道这种背包还能退的.... 我们用dp[ i ]表示选取若干个物品重量到达 i 的方案数. 如果我们g[ i ]表示不用第 x 个物品的, 然后选若干其他的物品到达 i 的方案数. if(i ...
- Vijos1755 靶形数独 Sudoku NOIP2009 提高组 T4 舞蹈链 DLX
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求这个数独中所有的解法中的最大价值. 一个数独解法的价值之和为每个位置所填的数值 ...