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\)为右端点的回文 ...
随机推荐
- HTML&javaSkcript&CSS&jQuery&ajax(八)
一. <!DOCTYPE html><html><head><meta charset="utf-8"><tiitle> ...
- Mesh无线网络的定义与WiFi的区别
Mesh无线网络的定义与WiFi的区别 无线Mesh网络(无线网状网络)也称为「多跳(multi-hop)」网络,它是一种与传统无线网络完全不同的新型无线网络技术.无线网状网是一种基于多跳路由,对等网 ...
- python Com接口测试
import comtypes.client as cc import comtypes tlb_id = comtypes.GUID("{D85C6069-D628-4276-93C3-9 ...
- C:\Users\Administrator\.IntelliJIdea2017.3\system\tomcat\Unnamed_08_upload\work\Catalina\localhost\ROOT这个错误是找不到temp文件
C:\Users\Administrator\.IntelliJIdea2017.3\system\tomcat\Unnamed_08_upload\work\Catalina\localhost\R ...
- spring quartz整合实现定时器自动注解
1.web.xml中添加侦听器 <listener> <listener-class>org.springframework.web.context.ContextLoa ...
- 解决bootstrap多模态框跳转时页面左移问题
衍生问题暂未发现.... 忽略左右跳动视觉差 解决方法: 在bootstrap的js搜索padding-right,然后找到“+this.scrollbarWidth”,删掉即可.
- 有道词典Linux版下载安装
http://cidian.youdao.com/index-linux.html Ubuntu http://codown.youdao.com/cidian/linux/youdao-dict_1 ...
- 深入理解JSCore
https://blog.csdn.net/MeituanTech/article/details/82108667
- Git基础(一) 创建项目仓库
一.取得Git项目仓库的两种方法: 在现有目录中初始化仓库 git init 如果是在一个已经存在文件的文件夹(而不是空文件夹)中初始化Git仓库来进行版本控制的话,应该跟踪这些文件并提交,通过git ...
- golang 的glide包管理使用技巧教程
安装glide ➜ wemall git:(master) ✗ go get github.com/Masterminds/glide ➜ wemall git:(master) ✗ go insta ...