Aizu2292 Common Palindromes
题意
我也不知道哪里来的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的更多相关文章
- 【Aizu2292】Common Palindromes(回文树)
[Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...
- 回文树/回文自动机(PAM)学习笔记
回文树(也就是回文自动机)实际上是奇偶两棵树,每一个节点代表一个本质不同的回文子串(一棵树上的串长度全部是奇数,另一棵全部是偶数),原串中每一个本质不同的回文子串都在树上出现一次且仅一次. 一个节点的 ...
- URAL Palindromic Contest
A. Non-palidromic cutting 考虑无解的情形:只能是形如$aaaaa$.$aaabaaa$.$abababa$这三种情况. 有解时,对于最小划分,答案必定是$1$或者$2$,判断 ...
- Socket聊天程序——Common
写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- [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 ...
- [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 ...
随机推荐
- 使用HttpClient出现java.io.IOException: Attempted read from closed stream
问题描述: 使用httpClient时候,出现java.io.IOException: Attempted read from closed stream. 原始代码: public static S ...
- How to manage local libraries in IntelliJ IDEA
如何在 IntelliJ IDEA 中管理本地类库 一般来说,如果项目是基于 Maven 管理工具的,我们会在 pom.xml 中添加 dependency 来管理依赖.但有时也会遇到要用的类库不在 ...
- java源码解析之Object类
一.Object类概述 Object类是java中类层次的根,是所有类的基类.在编译时会自动导入.Object中的方法如下: 二.方法详解 Object的方法可以分成两类,一类是被关键字fin ...
- css的reset和常用的html标签的默认样式整理
先看下常用的这些标签浏览器的默认样式有哪些: body{ margin: 8px;} hr{ border:1px inset; margin-top:.5em;margin-bottom:.5em; ...
- activity启动模式launchMode区别和优化
初学android的开发人员,可能会经常忽略这个重要的设置. Activity一共有以下四种launchMode:1.standard2.singleTop3.singleTask4.singleIn ...
- linux下RabbitMQ相关命令
1. 关闭与启动 ① 到指定目录:cd/etc/init.d ② 停止:rabbitmq-server stop ③ 启动:rabbitmq-server start ④ 查看是否停止/启动成功:ps ...
- linux通过源码安装nodejs
1.下载nodejs源码,下载地址:https://nodejs.org/en/download/ 2.下载回来后,解压缩,ubuntu系统ctrl + alt +T 打开终端 假如tar.gz压缩包 ...
- Struts2 Servelet重构
这是利用action模仿请求Servelet(单例) 作用: 1.减少web.xml代码量 2.将servelet中的代码转移到action中,只需要在action中定义业务逻辑则可. 1.定义一个过 ...
- PHP之mb_strripos使用
mb_strripos (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_strrpos - Find position of last occurrence of a str ...
- IDEA 启动项目,tomcat中配置的虚拟路径无法使用
有时候,使用idea启动项目,非动静分离项目,直接根据图片url地址显示图片,会发现图片无法显示,tomcat中配置的虚拟路径无法使用,这时候需要配置idea.选择路径,然后给与一个访问名就行了.