【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 ...
随机推荐
- sendcloud golang 发送短信 示例代码
package main import ( "fmt" "crypto/md5" "encoding/hex" "sort&quo ...
- Numpy入门笔记第二天
# 数组的组合 import numpy as np arr1 = np.arange(5) arr2 = np.arange(3) print arr1 print arr2 [0 1 2 3 4] ...
- Python异常(基础) except
为什么要异常处理机制:在程序调用层数较深时,向主调函数传递错误信息需要层层return 返回比较麻烦,用异常处理机制可以较简单的传送错误信息 什么是错误 错误是指由于逻辑或语法等导致一个程序已无法正常 ...
- java引用Arcface,实现人脸识别(demo)
## 开发环境准备: ###开发使用到的软件和工具: * Jdk8.mysql5.7.libarcsoft_face.dll(so).libarcsoft_face_engine.dll(so).li ...
- PytorchZerotoAll学习笔记(四)--线性回归
线性回归 # 导入 torch.torch.autograd的Variable模块import torch from torch.autograd import Variable # 生成需要回归需要 ...
- 从无到有之webpack+vuerouter的简单例子以及各个属性解释
之前一直没玩过webpack和vue,近两周才看这玩意,本文纯属自己的实验+之前angular作战经验的理解一些入门文章 首先webpack关于vue以及各个包 module.exports = { ...
- 【探路者】团队Alpha周贡献分数分配结果
经本组成员商议,根据老师提供的分数,(每人携带10分进入团队,[探路者]团队7人,共计35分). 本周每位同学携带10分进入组内,7人共计70分.分数公布如下: 吴雨丹 15分 贾雅杰 12分 蔺依铭 ...
- 常用的比较器:实现方式Compareable和Comparator
class Dog{ int size; int weight; public Dog(int s, int w){ size = s; weight = w; } } 目的:对于Dog对象作为元素所 ...
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- spring复杂数据类型传递
1.VO对象与PO对象的区别: PO(persistant object) 持久对象:通常对应数据模型(数据库),本身还有部分业务逻辑的处理.可以看成是与数据库中的表相映射的java对象.最简单的PO ...