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 ...
随机推荐
- docker查看容器
1.查看启动成功的容器,这个命令看不见的说明已经炸了: # docker ps 2.查看所有容器,死的活的都能看见: # docker ps -a 3.查看容器日志: # docker logs c8 ...
- mac编辑器vim美化
mac编辑器vim美化 contents 环境 效果呈现 安装 quick start 环境 mac10.13.6,vim7(该版本mac自带的vim是7),git mac下vim的配置文件有两处 一 ...
- JAVA编程思想第一章——对象导论
- Add:四则运算
输入为四则运算表达式,仅由整数.+.-.*./ .(.) 组成,没有空格,要求求其值.假设运算符结果都是整数 ."/"结果也是整数 表达式 由 项 或 项 ...
- winform两个窗体之间传值(C#委托事件实现)
委托 定义一个委托,声明一个委托变量,然后让变量去做方法应该做的事. 委托是一个类型 事件是委托变量实现的 经典例子:两个winform窗体传值 定义两个窗体:form1和form2 form1上有一 ...
- centos7.x设置nginx开机自启动
设置nginx开机自启动(centos7.x) 第一步:进入到/lib/systemd/system/目录 [root@iz2z init.d]# cd /lib/systemd/system/ 第二 ...
- JavaScript引擎理解
JavaScript 虽然给人感觉是一个多线程执行的语言,但是其实JavaScript引擎是伪多线程,是单线程执行的, 浏览器内核:实现允许多个线程异步执行,这些线程在内核制控下相互配合以保持同步.假 ...
- [作业] Python入门基础--用户登陆
让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定登陆 #__author:Mifen #date: 2018/11/28 import time #自定义本地用户名和密码 user_nam ...
- GB2312编码(为什么要加2020H、8080H,外码→内码→交换码→字形码)
为什么要加上2020H和8080H? 区位码.内码.国标码怎么转换非常简单,但是令人迷惑的是为什么要那么转换?这种转换不可能平白无故地那样转换! 我搜索很多资料,找到最好的解释,总结如下: 首先,注意 ...
- background-position 详解
一.语法 语法:background-position:x, y; 定义:背景图片相对容器原点的起始位置: 取值: 关键字:top | center | bottom | left | cen ...