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 ...
随机推荐
- 算法导论-MIT笔记
第一部分 Analysis of Algorithms 算法分析是关于计算机程序性能(performance)和资源利用的理论研究 1 What's more important than perfo ...
- c常用函数
一.strtol long int strtol(const char *nptr, char **endptr, int base) strtol()会将nptr指向的字符串,根据参数base,按权 ...
- appium安装与部署
前提: ①:appium属于C/S架构,代码写在Client端 ②:本章所说的部署讲的是Android设备下的Appium安装与部署 ③:Appium Client的环境是针对python3的 App ...
- Prufer序列与树的计数(坑)
\(prufer\)序列: 无根树转\(prufer\)序列: 不断找编号最小的叶子节点,删掉并在序列中加入他相连的节点. \(prufer\)转无根树: 找到在目前\(prufer\)序列中未出现且 ...
- elastic-job动态添加定时任务
在elastic-job的使用过程中,我们会遇到动态添加定时任务的时候,但是官网上面并没有对这块内容进行说明.按照我的理解以及官网上面elastic-job的框架图,ej的定时任务其实是存储在zook ...
- PHP之string之chr()函数使用
chr (PHP 4, PHP 5, PHP 7) chr - Return a specific character chr - 返回指定的字符 Description string chr ( i ...
- 错误:‘lock_guard’ 在此作用域中尚未声明
解决:修改报错文件,加入#include <boost/thread/lock_guard.hpp>
- tmux快捷键汇总(常用)
会话外操作: tmux new -s <name-of-my-session> 在会话外创建一个新的会话 tmux ls 在会话外获取会话列表 tmux a(attach) -t < ...
- java class load 类加载
1:what is it jvm把描述类的数据从class字节码文件加载到内存,并对数据进行校验.解析.初始化,最终成为jvm直接使用的数据类型 1.ClassNotFoundExcetpion ...
- 利用css实现搜索过滤
无意中找到一种利用css就可实现的搜索过滤的方法,不得不说看了代码之后确实被惊艳到了,亏我之前面试还因为做这个功能做太慢而拖了后腿.在此记录下代码: <!DOCTYPE html> < ...