题目链接


\(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. cf里的一些简单组合数题

    cf711D 成环的和不成环的要单独计算,环用双联通做的QAQ /* 所有情况-成环的情况 */ #include<bits/stdc++.h> using namespace std; ...

  2. Python实现switch效果

    Java中有switch这个东东有的地方使用switch感觉还挺好使,但是Python没有提供switch这个东东,下面我们想办法来完成类似Java和C里面的那种switch效果. Java示例代码: ...

  3. jQuery示例

    <!DOCTYPE html><html lang="en" class="loading"><head> <meta ...

  4. nginx+tomcat实现集群,redis实现session共享,软连接实现文件共享:http://blog.csdn.net/hua1586981/article/details/78132710

    转载 2017年02月08日 16:52:41 730 相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能 ...

  5. 如何使用VisualSVN Server建立版本库

    首先打开VisualSVN Server Manager,如图: 可以在窗口的右边看到版本库的一些信息,比如状态,日志,用户认证,版本库等.要建立版本库,需要右键单击左边窗口的Repositores, ...

  6. Spring.Net 简单实例-02(属性注入)

    说明:接续Spring.Net 简单实例-01(IOC) 话不多说看操作 1:为UserInfo添加属性 2: 修改App.config中代码 <?xml version="1.0&q ...

  7. python中深拷贝与浅拷贝

    # 1.浅拷贝(复制东西)a = [11,22,33] # 实际上是浅拷贝# 没有把这个变量的值赋进去,而是把另一个变量的地址拿过去了,就叫浅拷贝.b = a # print(id(a))# prin ...

  8. PAT Basic 1069. 微博转发抽奖(20)

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  9. ubuntu中vim下按上下左右键时输入A、B、C、D

    ubuntu系统自带的 vi 不完整导致,解决方法:安装完整的vi $ sudo apt-get install vim-gtk 1. 为root用户设置密码 sudo passwd root 需要先 ...

  10. MyBatis - 8.MyBatis工作原理

    Mybatis 配置 1.SQLSessionFactory的初始化 根据配置文件获取 SqlSessionFactory 2.openSession获取SqlSession对象 3.getMappe ...