CodeForces 17E Palisection (回文树)

浅析马拉车Manacher算法(附Codeforces 17E Palisection) 引入 在字符串问题处理中,常常会遇见处理回文串的问题。那么首先就要介绍一下什么是回文串——回文串就是对于一个字符串, 阅读详情

Palisection
time limit per test2 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard output
In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», «racecar», «rotor», «madam».

Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.

Let’s look at the actions, performed by Nick, by the example of text «babb». At first he wrote out all subpalindromes:

• «b» — 1..1
• «bab» — 1..3
• «a» — 2..2
• «b» — 3..3
• «bb» — 3..4
• «b» — 4..4
Then Nick counted the amount of different pairs among these subpalindromes that cross. These pairs were six:

  1. 1..1 cross with 1..3
  2. 1..3 cross with 2..2
  3. 1..3 cross with 3..3
  4. 1..3 cross with 3..4
  5. 3..3 cross with 3..4
  6. 3..4 cross with 4..4
    Since it’s very exhausting to perform all the described actions manually, Nick asked you to help him and write a program that can find out the amount of different subpalindrome pairs that cross. Two subpalindrome pairs are regarded as different if one of the pairs contains a subpalindrome that the other does not.

Input
The first input line contains integer n (1 ≤ n ≤ 2·106) — length of the text. The following line contains n lower-case Latin letters (from a to z).

Output
In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987.

Examples
input
4
babb
output
6
input
2
aa
output
2

先处理出以某个位置为结尾的回文串数量,以及从某个位置到结束的所有回文串的数量。用回文串两两搭配的总数-所有拼接起来不是回文串的数量就是答案。

#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
#include "vector"
#define MOD 51123987
using namespace std;
const int MAXN = 2000005 ;
//const int N = 26 ;
char str[MAXN];
struct Palindromic_Tree {
    vector<pair<int,int>> next[MAXN] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
    //int cnt[MAXN] ;
    int num[MAXN] ;
    int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
    int S[MAXN] ;//存放添加的字符
    int last ;//指向上一个字符所在的节点,方便下一次add
    int n ;//字符数组指针
    int p ;//节点指针

    int newnode ( int l ) {//新建节点
        next[p].clear();
        //cnt[p] = 0 ;
        num[p] = 0 ;
        len[p] = l ;
        return p ++ ;
    }

    void init () {//初始化
        p = 0 ;
        next[0].clear();
        next[1].clear();
        newnode (  0 ) ;
        newnode ( -1 ) ;
        last = 0 ;
        n = 0 ;
        S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
        fail[0] = 1 ;
    }

    int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
        while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;
        return x ;
    }
    int find(int cur,int c)
    {
        for(int i=0;i<next[cur].size();i++)
        {
            if(next[cur][i].first==c)
                return next[cur][i].second;
        }
        return 0;
    }

    int add ( int c ) {
        c -= 'a' ;
        S[++ n] = c ;
        int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
        int k=find(cur,c);
        if ( k==0 ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
            int now = newnode ( len[cur] + 2 ) ;//新建节点
            fail[now] = find(get_fail ( fail[cur] ),c) ;//和AC自动机一样建立fail指针,以便失配后跳转
            next[cur].push_back(make_pair(c,now));
            num[now] = num[fail[now]] + 1 ;
        }
        last = find(cur,c) ;
        //cnt[last] ++ ;
        return num[last];
    }
   /*
    void count () {
        for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }*/
} tree;

long long sum[MAXN],tail[MAXN];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        memset(tail,0,sizeof(tail));
        scanf("%s",str+1);
        tree.init();
        for(int i=1;i<=n;i++)
        {
            tail[i]=tree.add(str[i]);
        }
        tree.init();
        for(int i=n;i>=1;i--)
        {
            sum[i]=((sum[i+1]+tree.add(str[i]))%MOD)%MOD;
        }
        long long ans=sum[1]*(sum[1]-1)/2%MOD;
        for(int i=2;i<=n;i++)
            ans=(ans-tail[i-1]*sum[i]%MOD)%MOD;
        ans=(ans+MOD)%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}
Wireshark 4.6 TCP 流图解析:3种拥塞控制算法可视化对比 本文深入解析Wireshark 4.6的TCP流图功能,通过可视化对比Reno、Cubic和BBR三种主流拥塞控制算法的性能特征。文章提供详细的实验环境搭建方法、流图分析工具使用技巧,以及算法调优建议,帮助网络工程师优化TCP性能。 阅读详情

相关推荐

深入AOSP构建系统:从BUILD_FINGERPRINT的生成逻辑,看如何定制你的Android设备标识

本文深入解析AOSP构建系统中的设备指纹生成机制,详细介绍了BUILD_FINGERPRINT的生成逻辑及其在Android设备中的关键作用。通过分析多个指纹属性的区别与联系,以及如何定制设备标识,帮助开发者更好地理解和掌握Android系统构建过程。

weixin_27017211的博客 229

Codeforces 17E Palisection Manacher ( 或 Panlindromic Tree ) + 前缀和

题目大意: 就是现在给出一个长度为n的字符串( 1 大致思路: 继续刷manacher的论文题... 刚开始看到这个题的时候我想的是觉得可以正面做, 然后发现逗比了...因为我对于每一个以 i 位置为中心的半径为R[i]回文串只算了一次, 错以为可以树状数组弄一下...然后逗比地美国样例之后发现, 以 i 为中心的回文串有R[i]个( i 处不是 '#' 的情形下)... 然后看

Gatevin的专栏 1555

centos7 清理缓存

一、系统环境 系统版本 CentOS Linux release 7.3.1611 (Core) 内核版本 Linux xx-user 3.10.0-514.el7.x86_64 #1 SMP 背景 服务器分配的账号是普通用户,只能执行sudo命令 二、缓存查看及基本概念 查看内存使用情况 [user01@xx-user ~]$ free -h total used free shared buff/cache availabl

sunzq55的博客 4012

CodeForces 17E Palisection(manacher算法+递推)

题意:给你一个长度为n的序列,问你有多少相交的回文串。 做法:可知求相交的比较难,但是求不相交的却很简单,先用manacher算法o(n)的求出每个点的回文串最长有多长。再求出start,end数组,分别代表以i为开头的回文串有几个,以i为结尾的回文串有几个。这可以利用标记再加上前缀和求,最后要求的不相交个数是:sum( start[j] * sum(end[i]| 1 PS:因为n高达2*1

glq007的acm的奋斗 1687

GPU基准测试全攻略:从驱动验证到深度学习环境部署

GPU基准测试是评估显卡性能与稳定性的核心方法,其原理在于通过标准化负载程序,对GPU的计算单元、显存和散热系统进行压力测试。这项技术的价值在于,它能科学量化硬件性能,确保软件环境配置正确,为深度学习、科学计算等重负载任务提供稳定基础。在实际应用场景中,无论是新卡验机、排查深度学习环境问题(如“warning: no usable gpu found”报错),还是验证“PyTorch GPU版本安装”后的实际算力,系统的基准测试都是不可或缺的工程实践环节。通过结合通用计算测试与深度学习框架验证,用户可以提前

weixin_30080745的博客 347

Win10 IPv6 远程桌面连接(小米路由器)

背景 由于家里是移动网,且懒得向运营商要公网IPv4,所以想着用IPv6试一下。 家里的环境是这样的: 宽带用的是移动的,没有公网IPv4,但经过测试发现IPv6是公网的 上网通过光猫拨号,而不是通过路由器 路由器是小米AC2100 光猫更改为桥接模式 路由器必须要通过PPPoE拨号,如果是光猫拨号,那么要找对应的维护人员辅助你把光猫改成桥接模式。通过PPPoE拨号路由器就能够拿到公网的IPv6网段,也就可以给底下的其他服务分配公网IPv6了。而如果是光猫拨号,那么只有路由器可以拿到公网的IP。其他的设

timczm的博客 1万+

CodeForces - 17E Palisection 邻接表的回文树

题目:给出一个字符串要求找出多少对有相交部分的回文子串。 思路:直接求相交的不好算,我们可以用总对数减去不想交的回文串的对数。 Manacher算法: 先利用差分数组统计出以输入串的第i个字符结尾的回文串的数量sum1[i],然后令sum2[i] = sum2[i - 1] + sum1[i]作为sum1的前缀和, 那么对于中心为i半径为R[i]的回文串来说, 处在这组回

ACVevtor的博客 473

vscode+keil嵌入式软件开发全流程

vscode+keil嵌入式软件开发全流程

AspringQ的博客 6812

CodeForces 17E

CodeForces 17E 题目 17E 分析 回文树裸题 这题由于空间的限制,所以要使用邻接表的形式。。 转换一下就成了回文树裸题了。 求相交的回文串个数。。 转换:求相交的个数 === 总个数 −-− 不交的个数 求回文串的总个数:回文树可以解决 回文树可以求到 iii 的回文串个数 A[i]A[i]A[i] ,把串反着插入回文树中就得到从 iii 开始的回文串个数 B[i]B[i]...

strategist_614的博客 266

YOLOv8中的C2f模块代码详解

在YOLOv8网络结构中,C2f模块(CSP Bottleneck with 2 Convolutions)是一个关键组件,用于实现跨阶段部分聚合

zzzyyy8的博客 3万+

AgentScope深入分析-ReActAgent

ReActAgent 是 AgentScope 框架中最核心的智能体实现,它完美诠释了"推理-行动"(Reasoning-Acting)循环的精髓。本文将深入分析 ReActAgent 的类继承关系、核心执行流程,以及状态管理、钩子机制、实时介入等关键技术点。通过阅读本文,你会理解智能体如何与模型交互、如何执行工具、如何处理中断,以及这些机制背后的设计考量。模块化:通过组合不同的组件(模型、格式化器、工具集、记忆)来构建智能体透明性:所有关键流程都对开发者可见,可以精确控制可扩展性。

846

codeforces 17E

题目链接 题意 给定一个长度为n的小写字母串。问你有多少对相交的回文子 串(包含也算相交) 。 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串个数%51123987 Translated by liyifeng 解法 因为首先是回文串,所以考虑manacher,求出每个节点的f[i],然后考虑直接求相交的回文串对数量不是很好求,所以用...

xiayuyang 318

AI 实战篇 |基于 AI开放平台实现 人脸识别对比 功能,超详细教程【附带源码】

本篇文章来学习一下怎样使用 Unity实现简单地人脸识别 功能。 关于人脸识别,我们可以选择很多平台的SDK接入Unity来实现人脸识别的功能。

努力前行,总会成为自己心中的那道光 5万+

CodeForces 17E Palisection(回文树)

CodeForces 17E Palisection(回文树)

Dacc123的博客 1171

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Codeforces 17E Palisection 回文树

题意 给出一个长度为n的字符串S,问S有多少个回文子串存在相交关系。 n&lt;=2000000 分析 正难则反,考虑如何求不相交的回文子串数量。 通过回文树求出f[i]表示以i为结尾的回文子串数量,g[i]表示以i为开头的回文子串数量。 然后答案就是∑ni=1f[i]∗∑nj=i+1g[j]∑i=1nf[i]∗∑j=i+1ng[j]\sum_{i=1}^nf[i]*\sum_{j...

beginend 366

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Palisection CodeForces - 17E (回文树)

题意:给一个字符串,然后求相交的回文串的对数。 思路:求出总对数,然后利用num[i]求出不相交的对数减去即可。 顺便学了mod非质数下的逆元求法,要用扩展欧几里得求,但是也有限制要分母与mod互质才行 #include<bits/stdc++.h> using namespace std; #define ls rt<<1 #define rs (rt<&...

SEIZE THE DAY 254

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

CodeForces - 17E E. Palisection(回文树)

In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way b...

Cymbals的博客 410

CodeForces - 17E - Palisection ( 回文树 )

题目链接:点击进入 题目 题意 对一个给定字符串,求有多少对回文子串有交集 思路 求出这个字符串所有回文子串的对数,然后 所有的 - 无交集的 = 有交集的。 求所有的: 顺序建立回文树,用 sum 数组,通过前缀和,来维护前 i 位总共有多少回文串。 顺序枚举每个位置,以当前位置为末尾的回文串个数 cnt 乘 当前位置之前的回文串个数 sum [ i - 1 ] ,加上当前位置自身的贡献 cnt * ( cnt -1 ) / 2 ,累加答案 ans1 。 求无交集的: 逆序建立回文树,逆序枚举每个

Cosmic_Tree的博客 310

Codeforces Beta Round #17 E - Palisection 回文树写法

这一题很卡空间,如果用回文数的话。 我们可以用vector代替 next数组。 省20倍空间大概。 直接算相交回文串很难。 但算不相交回文串很简单。 我们知道,用回文树,能维护出以i结尾的回文串的个数。 再倒着维护一边字符串,就知道以i开头的回文串的个数。 然后记录前缀和直接算即可。 全部的回文串个数更简单。。用cnt数组或者直接用我们的前缀和也行 可以节省空间。 ...

bjfu170203101的博客 207

Codeforces Beta Round #17 E. Palisection(回文树)

E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard input output standard output In an English class Nick had nothing to do a

u013268685的专栏(有一种幸福叫AC,有一种期待叫AK) 1018

Palisection(Codeforces Beta Round #17E+回文树)

题目链接 传送门 题意 给你一个串串,问你有多少对回文串相交。 思路 由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数。 而不相交的回文对数可以通过计算以\(i\)为右端点的回文串的个数\(\times\)以\(i+1,i+2\dots,n\)为左端点的回文串的个数计算得到。 以\(i\)为右端点的回文串的个数可以直接用回文树\(O(n)\)求出来,以\(i\)为左...

di6499的博客 132

Codeforces 17E Palisection 【Manacher】

Codeforces 17E Palisection E. Palisection In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is call...

Dream_maker的博客 385

CodeForces 17E Palisection (回文自动机)

题意:求字符串所有回文子串相交对数。 题解:回文自动机 要求相交对数,那么先求出对数的总数,然后减去不相交的对数就好了。 sum[]sum[]sum[]:从iii开始往后的回文子串个数。从后往前更新。 add()add()add()返回值:以iii结尾的回文子串个数。 总对数−Σ(sum()∗add())- Σ(sum()*add())−Σ(sum()∗add()) 就是答案了。 然后就是nex...

gongyuandaye的博客 272

CodeForces - 17E Palisection (回文自动机)

n an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way b...

Top_Spirit的博客 278

F - Palisection CodeForces - 17E

In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way ...

qq_43690454的博客 168
上一篇: HDU 5658 CA Loves Palindromic (回文树)
下一篇: POJ 3264 Balanced Lineup (线段树)
MrBlankIsAwesome
博客等级 码龄11年 4粉丝 196原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值