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\)为右端点的回文 ...
随机推荐
- 网络扫描信息收集基于(Windows)
1.首先说明一下一款网络扫描工具,在之前的博客中我曾简要的写过关于Advance IP Scanner使用方法,最近要写网络扫描的工具,所以对这款工具做一个详细的功能细节上的介绍. 如下图 在输入框 ...
- Appium Desired Capabilities
Appium Desired Capabilities Desired Capabilities 是由 keys 和 values 组成的 JSON 对象. 举个简单例子: { "platf ...
- servlet保存会话数据---利用隐藏域
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...
- windows_agent 添加
一:复制windows agent文件和.exe文件到c:\zabbix\目录下 二:配置zabbix_agentd.win.conf文件 hostname:设置为自定义名称,但是要和zabbix-s ...
- 关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法
关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法 1. 找到以下路径 ...\Kingdee\K3ERP\KDHR\SITEFILE\WEBUI\ 找到“Login.aspx” ...
- weixinShare.js / 极简微信分享插件
weixinShare.js / 极简微信分享插件 / 版本:0.1 这是一个很简单.很实用的微信分享插件,无需jQuery,只需要在网页里加入一行JS代码,即可自动识别微信浏览器并启动微信分享的提示 ...
- android app使用微信登录接口回调没有被执行的问题研究
本人开发的一个app使用了sharesdk集成微信登录功能,在测试的过程中微信授权登录界面有调用,但是授权后原应用的回调没有被执行 应用的包名是com.kimi.searcher 首先,确认微信点击授 ...
- VS2015 生成事件 命令参数
来源:https://stackoverflow.com/questions/11001822/copy-files-from-one-project-to-another-using-post-bu ...
- [转]编程珠玑第五章二分搜索(折半查找)之java实现
http://blog.csdn.net/hwe_xc/article/details/51813080 二分搜索又称为折半查找,用来高效快速的解决如下问题: 我们需要确定排序后的数组x[0..n-1 ...
- [转]java位运算(1)
http://blog.csdn.net/xiaochunyong/article/details/7748713 Java提供的位运算符有:左移( << ).右移( >> ) ...