题目链接


\(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(回文树)的更多相关文章

  1. 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 ...

  2. Codeforces 932G Palindrome Partition - 回文树 - 动态规划

    题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...

  3. Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)

    layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...

  4. UVALive - 7041 The Problem to Slow Down You (回文树)

    https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...

  5. UVAlive 7041 The Problem to Slow Down You(回文树)

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  6. 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 ...

  7. 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 ...

  8. CodeForces 17E Palisection(回文树)

    E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...

  9. Palisection(Codeforces Beta Round #17E+回文树)

    题目链接 传送门 题意 给你一个串串,问你有多少对回文串相交. 思路 由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数. 而不相交的回文对数可以通过计算以\(i\)为右端点的回文 ...

随机推荐

  1. vue 中样式的绑定

    1.class的对象绑定 //对应的css <style> .active { color: red; } </style> <!--html 对应的代码--> & ...

  2. Nginx详解五:Nginx基础篇之HTTP请求

    http请求 如今的http请求已经不是每一次请求都进行一次三次握手,可以在请求与相应之后,客户端和服务端不断的发送FIN和ACK包来保持连接的状态,即:长连接 HTTP请求建立在一次TCP连接基础上 ...

  3. 在vue项目中使用axios发送FormData

    这个是axios的中文文档,挺详细的: https://www.kancloud.cn/luponu/axios/873153 文档中的    使用 application/x-www-form-ur ...

  4. ajax beforeSend 写的显示隐藏代码不执行

    ajax如果要写像下方格式 $.ajax({ url: ajaxurl, type: 'POST', dataType: 'json', async:true, data: { }, beforeSe ...

  5. Python os.walk() 方法遍历文件目录

    概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下. os.walk() 方法是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 在Un ...

  6. java数组元素的复制

    package day03; import java.util.Arrays; /** * * 数组元素的复制: int的默认值是0,boolean默认值是flase 数组的扩容和缩容(本质的实现数组 ...

  7. 目标检测算法之YOLOv3

    参考地址:https://blog.csdn.net/leviopku/article/details/82660381 YOLO v3结构图 DBL:卷积+BN+leaky relu,是v3的最小组 ...

  8. OpenCV-Python入门教程4-颜色空间转换

    一.颜色空间转换 import cv2 import numpy as np img = cv2.imread('lena.jpg') # 转换成灰度图 img_gray = cv2.cvtColor ...

  9. python 0007

    #coding='utf-8'# import re import os def get_list(): list_file=[] for f in os.listdir(): if f.endswi ...

  10. python---顺序查找,二分查找

    比较熟悉了. 但要注意细节, 二分查找时,普通方法mid处理,递归时,mid处理. # coding = utf-8 def sequential_search(a_list, item): pos ...