【Aizu2292】Common Palindromes(回文树)
【Aizu2292】Common Palindromes(回文树)
题面
Vjudge
神TMD日语
翻译:
给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数
满足\(S[i,j],T[k,l]\)都是回文串并且\(S[i,j]=T[k,l]\)
题解
自己\(yy\)一下就会做了
回文树又叫做回文自动机,所以当然可以用来进行回文串的识别和匹配了
对于一个串构建\(PAM\)或者说回文树,统计一下每个回文串的出现次数
再用另外一个串在\(PAM\)上进行匹配,计算一下每个节点被访问的次数
最后把每个节点的两个值乘起来求和就行了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 55555
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct PAM
{
struct Node{int son[26],len,ff,v;}t[MAX];
int tot,last;
void init(){t[tot=1].len=-1;t[0].ff=t[1].ff=1;}
void extend(int c,int n,char *s)
{
int p=last;
while(s[n-t[p].len-1]!=s[n])p=t[p].ff;
if(!t[p].son[c])
{
int v=++tot,k=t[p].ff;
while(s[n-t[k].len-1]!=s[n])k=t[k].ff;
t[v].len=t[p].len+2;
t[v].ff=t[k].son[c];
t[p].son[c]=v;
}
last=t[p].son[c];
t[last].v++;
}
}P;
int f[MAX];
char ch[MAX];
ll ans=0;
int main()
{
P.init();
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1);i<=l;++i)P.extend(ch[i]-65,i,ch);
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1),now=1;i<=l;++i)
{
int c=ch[i]-65;
while(now!=1&&(!P.t[now].son[c]||ch[i]!=ch[i-P.t[now].len-1]))now=P.t[now].ff;
if(P.t[now].son[c]&&ch[i]==ch[i-P.t[now].len-1]){now=P.t[now].son[c];f[now]++;}
else now=1;
}
for(int i=P.tot;i;--i)f[P.t[i].ff]+=f[i];
for(int i=P.tot;i;--i)P.t[P.t[i].ff].v+=P.t[i].v;
for(int i=1;i<=P.tot;++i)
ans+=1ll*f[i]*P.t[i].v;
printf("%lld\n",ans);
return 0;
}
【Aizu2292】Common Palindromes(回文树)的更多相关文章
- SPOJ Number of Palindromes(回文树)
Number of Palindromes Time Limit: 100MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu ...
- SP7586 NUMOFPAL - Number of Palindromes(回文树)
题意翻译 求一个串中包含几个回文串 题目描述 Each palindrome can be always created from the other palindromes, if a single ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...
- URAL 2040 Palindromes and Super Abilities 2(回文树)
Palindromes and Super Abilities 2 Time Limit: 1MS Memory Limit: 102400KB 64bit IO Format: %I64d ...
- CF245H Queries for Number of Palindromes(回文树)
题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...
- 回文树/回文自动机(PAM)学习笔记
回文树(也就是回文自动机)实际上是奇偶两棵树,每一个节点代表一个本质不同的回文子串(一棵树上的串长度全部是奇数,另一棵全部是偶数),原串中每一个本质不同的回文子串都在树上出现一次且仅一次. 一个节点的 ...
- CodeForces 17E Palisection(回文树)
E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...
随机推荐
- Android线程管理(一)——线程通信
线程通信.ActivityThread及Thread类是理解Android线程管理的关键. 线程,作为CPU调度资源的基本单位,在Android等针对嵌入式设备的操作系统中,有着非常重要和基础的作用. ...
- 【坚持】Selenium+Python学习之从读懂代码开始 DAY1
学习Selenium+Python已经好几个月了,但越学发现不懂的东西越多. 感觉最大的问题还是在于基础不扎实,决定从头开始,每天坚持读代码,写代码. 相信量变一定能到质变!!! 2018/05/09 ...
- 简述AQS原理
这是一道面试题:简述AQS原理 AQS核心思想是,如果被请求的共享资源空闲,则将当前请求资源的线程设置为有效的工作线程,并且将共享资源设置为锁定状态.如果被请求的共享资源被占用,那么就需要一套线程阻塞 ...
- jquery on函数和prop与attr区别
一.jquery on()方法 1.语法 2.例子 $(document).ready(function(){ $("p").on("click",functi ...
- DeepLearning - Regularization
I have finished the first course in the DeepLearnin.ai series. The assignment is relatively easy, bu ...
- 使用 Sublime Text 做 Javascript 编辑器 - 集成 JSHint 问题检测工具
JSHint(jshint.com)是 Javascritp 代码质量工具,可以帮助开发人员发现 Javascript 代码中的错误和潜在的问题.jshint.com 是一个在线编辑器,我们可以为 S ...
- 每日Scrum--No.2
Yesterday:找地图 Today: 找到最适合我们软件的地图版本 Problem:找不到特别匹配的版本
- Fiveplus--王者光耀1
**光耀101** 汇总博客: 关文涛: 博客地址:随笔1 随笔2 杨蓝婷: 博客地址:随笔1 随笔2 蔡雅菁: 博客地址:随笔1 随笔2 黄森敏: 博客地址:随笔1 随笔2 林兴源: 博客地址:随笔 ...
- lintcode-392-打劫房屋
392-打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自 ...
- 第7章 监听器Listener
Listener概述 Listener的使用 使用Listener需要实现相应的Listener接口. public class SessionListenerTest implements Http ...