POJ - 3376 Finding Palindromes manacher+字典树
题意
给n个字符串,两两拼接,问拼接后的\(n\times n\)个字符串中有多少个回文串。
分析
将所有正串插入字典树中,马拉车跑出所有串哪些前缀和后缀为回文串,记录位置,用反串去字典树中查询,两字符串拼成回文串有三种情况:
未匹配完,反串后缀是回文串。

匹配结束,正串后缀是回文串。

匹配结束,正串等于反串。

字典树中维护当前位置有多少正串和当前位置有多少后缀为回文串的正串。
Code
#include<cstring>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
#define fi first
#define se second
#define pb push_back
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e6+10;
int p[maxn*2],f[2][maxn],len[maxn],sum[2][maxn];
char s[maxn],t[maxn*2];
int son[maxn][26],tot;
ll ans;
void ins(int dl,int dr){
int rt=0;
for(int i=dl;i<=dr;i++){
if(f[1][i]) sum[1][rt]++;
if(!son[rt][s[i]-'a']) son[rt][s[i]-'a']=++tot;
rt=son[rt][s[i]-'a'];
if(i==dr) sum[0][rt]++;
}
}
void qy(int dl,int dr){
int rt=0;
for(int i=dr;i>=dl;i--){
if(f[0][i]) ans+=sum[0][rt];
if(!son[rt][s[i]-'a']) return;
rt=son[rt][s[i]-'a'];
if(i==dl) ans+=sum[1][rt]+sum[0][rt];
}
}
int mlc(int dl,int dr){
int m=0,p0=1;p[1]=1;t[++m]='#';
for(int i=dl;i<=dr;i++){
t[++m]=s[i];
t[++m]='#';
}
for(int i=2;i<=m;i++){
int j=min(p[p0]+p0-i,p[2*p0-i]);
if(i+j<p[p0]+p0){
p[i]=j;
}else{
while(i-j>=1&&i+j<=m&&t[i-j]==t[i+j]) ++j;
p[i]=j;p0=i;
}
}
for(int i=dl;i<dr;i++){
if(p[i-dl+2]-1==i-dl+1) f[0][i]=1;
if(p[dr-2*dl+i+3]-1==dr-i) f[1][i+1]=1;
}
}
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
int n;
scanf("%d",&n);int l=1;
for(int i=1,m;i<=n;i++){
scanf("%d%s",&len[i],s+l);
mlc(l,l+len[i]-1);
ins(l,l+len[i]-1);
l+=len[i];
}l=1;
for(int i=1;i<=n;i++){
qy(l,l+len[i]-1);
l+=len[i];
}
cout<<ans<<'\n';
return 0;
}
POJ - 3376 Finding Palindromes manacher+字典树的更多相关文章
- POJ 3376 Finding Palindromes EX-KMP+字典树
题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...
- POJ 3376 Finding Palindromes (tire树+扩展kmp)
很不错的一个题(注意string会超时) 题意:给你n串字符串,问你两两匹配形成n*n串字符串中有多少个回文串 题解:我们首先需要想到多串字符串存储需要trie树(关键),然后我们正序插入倒序匹配就可 ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
- POJ - 3376 Finding Palindromes(拓展kmp+trie)
传送门:POJ - 3376 题意:给你n个字符串,两两结合,问有多少个是回文的: 题解:这个题真的恶心,我直接经历了5种错误类型 : ) ... 因为卡内存,所以又把字典树改成了指针版本的. 字符串 ...
- POJ 3376 Finding Palindromes(manacher求前后缀回文串+trie)
题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...
- B - Finding Palindromes (字典树+manacher)
题目链接:https://cn.vjudge.net/contest/283743#problem/B 题目大意:给你n个字符串,然后问你将这位n个字符串任意两两组合,然后问你这所有的n*n种情况中, ...
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
随机推荐
- python学习-2 python安装和环境变量的设置
python的下载 1.可以去python官网下载,https://www.python.org/ 2.下载完成后,安装即可.(具体可以百度,网上都有很多安装方法) python的检测 1.打开开始- ...
- python保留字及其说明
保留字 说 明 and 用于表达式运算,逻辑与操作 as 用于类型转换 assert 断言,用于判断变量或条件表达式的值是否为真 break 中断循环语句的执行 class 用于定义类 con ...
- 牛客 128A 礼物 (组合计数)
大意: n种一元的奥利奥, m种2元的奥利奥, 求花恰好k元钱购买奥利奥的方案数. 可重组合问题, 直接dp即可. #include <iostream> #include <sst ...
- 怎样在Chrome浏览器上安装 Vue Devtools 扩展程序
第一步: 前往 GitHub 下载 Vue Devtools 项目文件 https://github.com/vuejs/vue-devtools 注意: 1. 将分支切换为 master 2. 下载 ...
- WebStorm使用码云插件问题
由于项目需求,需要在WebStorm中使用码云插件,在下载安装的过程中出现一系列的问题,现总结出现的问题和解决方法. 先说一下码云是什么?码云有什么作用? 码云的主要功能: 码云除了提供最基础的 Gi ...
- SPOJ-MobileService--线性DP
题目链接 https://www.luogu.org/problemnew/show/SP703 方法一 分析 很显然可以用一个四维的状态\(f[n][a][b][c]\)表示完成第i个任务时且三人 ...
- http、tcp简述
网络简述第一章 http.tcp简述 一.网络7层协议从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 : 其中高层(即7.6.5.4层)定 ...
- 使用XPath爬取网页数据
我们以我的博客为例,来爬取我所有写过的博客的标题. 首先,打开我的博客页面,右键“检查”开始进行网页分析.我们选中博客标题,再次右键“检查”即可找到标题相应的位置,我们继续点击右键,选择Copy,再点 ...
- 使用js输出1000以内的水仙花数
什么是水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯特 ...
- sql循环-游标、临时表、表变量
游标 在游标逐行处理过程中,当需要处理的记录数较大,而且游标处理位于数据库事务内时,速度非常慢. -- 声明变量 DECLARE @Id AS Int -- 声明游标 DECLARE C_Id CUR ...