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

  1. SPOJ Number of Palindromes(回文树)

    Number of Palindromes Time Limit: 100MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu ...

  2. SP7586 NUMOFPAL - Number of Palindromes(回文树)

    题意翻译 求一个串中包含几个回文串 题目描述 Each palindrome can be always created from the other palindromes, if a single ...

  3. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  4. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  5. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

  6. URAL 2040 Palindromes and Super Abilities 2(回文树)

    Palindromes and Super Abilities 2 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d ...

  7. CF245H Queries for Number of Palindromes(回文树)

    题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...

  8. 回文树/回文自动机(PAM)学习笔记

    回文树(也就是回文自动机)实际上是奇偶两棵树,每一个节点代表一个本质不同的回文子串(一棵树上的串长度全部是奇数,另一棵全部是偶数),原串中每一个本质不同的回文子串都在树上出现一次且仅一次. 一个节点的 ...

  9. CodeForces 17E Palisection(回文树)

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

随机推荐

  1. idea开启jquery提示及如何找到学习目标

    idea开启jquery提示 根据这些library就知道该学习哪些技术了

  2. Mac下重置MySQL密码

    第一步要先停止掉mysql服务: brew services stop mysql 第二步查看mysql安装路径: brew info mysql //我这里是brew管理的所以我用brew查看mys ...

  3. HTTP简单教程

    目录 HTTP简介 HTTP工作原理 HTTP消息结构 客户端请求消息 服务器响应消息 实例 HTTP请求方法 HTTP响应头信息 HTTP状态码 HTTP状态码分类 HTTP状态码列表 HTTP c ...

  4. selenium+Java,xpath定位方法详解(搬运留存)

    用xpath绝对路径比较费事费力,还容易报错,下面几种模糊定位比较灵活好用 driver.findElement(By.xpath("//*[@id='J_login_form']/dl/d ...

  5. intellij idea maven配置及maven项目创建

    1. 下载Maven 官方地址:http://maven.apache.org/download.cgi 解压并新建一个本地仓库文件夹 2.配置maven环境变量 3.配置配置本地仓库路径 4.配置阿 ...

  6. Javascript深入__proto__和prototype的区别和联系

    有一个一个装逼的同事,写了一段代码 function a(){} a.__proto__.__proto__.__proto__ 然后问我,下面这个玩意a.__proto__.__proto__.__ ...

  7. 【Python入门学习】闭包&装饰器&开放封闭原则

    1. 介绍闭包 闭包:如果在一个内部函数里,对在外部作用域的变量(不是全局作用域)进行引用,那边内部函数被称为闭包(closure) 例如:如果在一个内部函数里:func2()就是内部函数, 对在外部 ...

  8. Python常用模块之hashlib

    Python里面的hashlib模块提供了很多加密的算法,这里介绍一下hashlib的简单使用事例,用hashlib的md5算法加密数据 import hashlib hash = hashlib.m ...

  9. react native中state和ref的使用

    react native中state和ref的使用 因props是只读的,页面中需要交互的情况我们就需要用到state. 一.如何使用state 1:初始化state 第一种方式: construct ...

  10. ffmpe安装

    原文:https://www.jianshu.com/p/905df3d9e753 下载安装 下载最新源码包并解压 $ wget http://ffmpeg.org/releases/ffmpeg-3 ...