【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. 常用SQL语句大全(SQL Server)

    一.基础       查看数据库状态 select state_desc from sys.databases where name='dbname'  -- dbname数据库名 1.说明:创建数据 ...

  2. Google Chrome插件分享

    前言 浏览器是大家日常使用最多的工具之一,对于程序员来说,Google Chrome浏览器当然是大家优选的最爱之一.面对Chrome丰富的插件真的是爱不释手,如何把自己的Chrome调教成自己心仪的样 ...

  3. 【转】PHPCMS v9 自定义表单添加验证码验证

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

  4. Vue-lazyload 的使用

    Vue 项目使用 Vue-lazyload 做图片懒加载. 下载 下载 Vue-lazyload 的包NPM包 npm install vue-lazyload --save 引入 在项目 main. ...

  5. Python学习之web框架 Flask

    一.通过PIP 安装Flask 1.1 Windows环境安装pip A.首先PIP进入官网(https://pypi.python.org/pypi/pip)下载gz包 B.对gz压缩包进行解压,解 ...

  6. Amazon Headlines Update on Activity in US West Coast Ports

    According to news reports, freighter cargo may not be offloaded at U.S. West Coast ports from Februa ...

  7. Python3 迭代器和生成器

    想要搞明白什么是迭代器,首先要了解几个名词:容器(container).迭代(iteration).可迭代对象(iterable).迭代器(iterator).生成器(generator). 看图是不 ...

  8. $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 区别

    PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别 PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 通常情况下,PHP ...

  9. 作业MathExamV2.0

    MathExam233 211614269 林凯 211601233张康凌 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时( ...

  10. Java Web文件上传原理分析(不借助开源fileupload上传jar包)

    Java Web文件上传原理分析(不借助开源fileupload上传jar包) 博客分类: Java Web   最近在面试IBM时,面试官突然问到:如果让你自己实现一个文件上传,你的代码要如何写,不 ...