题意

我也不知道哪里来的OJ \(vjudge\) 上的

给定两个字符串 \(S,T\) ,询问 \((i,j,k,l)\) 这样的四元组个数

使得 \(S[i,j],T[k,l]\) 是相等的回文串

Sol

回文树

记录 \(S\) 的每个回文串的出现位置的集合大小

匹配 \(T\) 记录其每个回文串出现的位置集合大小

相乘即可

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll; IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} const int maxn(5e4 + 5); int son[26][maxn], len[maxn], num[maxn], fa[maxn], vis[maxn];
int tot, n, last;
char s[maxn], t[maxn]; IL void Extend(RG int pos, RG int c){
RG int p = last;
while(s[pos - len[p] - 1] != s[pos]) p = fa[p];
if(!son[c][p]){
RG int np = ++tot, q = fa[p];
while(s[pos - len[q] - 1] != s[pos]) q = fa[q];
len[np] = len[p] + 2, fa[np] = son[c][q];
son[c][p] = np;
}
last = son[c][p], ++num[last];
} int main(){
scanf(" %s %s", s + 1, t + 1), n = strlen(s + 1);
fa[0] = fa[1] = tot = 1, len[1] = -1;
for(RG int i = 1; i <= n; ++i) Extend(i, s[i] - 'A');
n = strlen(t + 1);
for(RG int i = 1, nw = 1; i <= n; ++i){
RG int c = t[i] - 'A';
while(nw != 1 && (!son[c][nw] || t[i - len[nw] - 1] != t[i])) nw = fa[nw];
if(son[c][nw] && t[i - len[nw] - 1] == t[i]) nw = son[c][nw];
++vis[nw];
}
for(RG int i = tot; i; --i) num[fa[i]] += num[i], vis[fa[i]] += vis[i];
RG ll ans = 0;
for(RG int i = 2; i <= tot; ++i) ans += 1LL * num[i] * vis[i];
printf("%lld\n", ans);
return 0;
}

Aizu2292 Common Palindromes的更多相关文章

  1. 【Aizu2292】Common Palindromes(回文树)

    [Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...

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

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

  3. URAL Palindromic Contest

    A. Non-palidromic cutting 考虑无解的情形:只能是形如$aaaaa$.$aaabaaa$.$abababa$这三种情况. 有解时,对于最小划分,答案必定是$1$或者$2$,判断 ...

  4. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  5. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  6. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  7. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. python数据类型详解(全面)

    python数据类型详解 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8.字典9.日期 1.字符串1.1.如何在Python中使用字符串a.使用单引号(')用单引号括起来表示字 ...

  2. jscover使用说明-总体说明

    1.总体说明 这个文档现在是完善和准确的,不管怎样,尽量去参考JSCoverage documentation. 1.1.介绍 JSCove是一个用来显示JavaScript项目代码覆盖率的工具,它是 ...

  3. spring属性配置执行过程,单列和原型区别

    Spring配置中,采用属性注入时,当创建IOC容器时,也直接创建对象,并且执行相对应的setter方法 Student.java package com.scope; public class St ...

  4. 最新版IntelliJ IDEA2019.1破解教程(2019.04.08更新)

    [原文链接]:https://www.tecchen.xyz/idea-crack.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力有限,对文章 ...

  5. FJOI2019 游记[大概是考完会解封?]

    Day -1 不知不觉就快省选了啊...从NOIP的爆炸到现在也已经过了两个月了啊... 因为高一没有封闭[划] 所以这些内容是翘了课[划]在机房写的 嘛...感觉自己还有好多东西不会啊……这两天一直 ...

  6. (五)Audio子系统之AudioRecord.stop

    在上一篇文章<(四)Audio子系统之AudioRecord.read>中已经介绍了AudioRecord如何获取音频数据,接下来,继续分析AudioRecord方法中的stop的实现 函 ...

  7. 我3年前开发的IM即时通讯一直没勇气推出,现在智能时代了,有什么可以结合的地方吗?

    我3年前开发的IM即时通讯一直没勇气推出,现在智能时代了,有什么可以结合的地方吗? 服务端采用基于XMPP协议的Openfire(当然改造了很多,也添加了很多握手协议) 客户端有做了四个版本:一个Ja ...

  8. redis 数据类型 Hash

    Redis 数据类型Hash:hash数据类型存储的数据和mysql数据库中存储的一条记录很类似. hash的一些操作: 比如数据库是user表,有id,name,age ,sex,可以建立与之对应的 ...

  9. 利用js对象的特性,去掉数组中的重复项

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  10. Ubuntu 下 unzip用法

    unzip [参数] <压缩文件> 参数: -P <密码> zip 压缩包的密码-f 覆盖原有文件-d <路径> 指定解压路径-n 解压缩时不覆盖原有文件-o 不经 ...