CF17E Palisection(回文树)

题意翻译

给定一个长度为n的小写字母串。问你有多少对相交的回文子 串(包含也算相交) 。 输入格式

第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式

相交的回文子串个数%51123987

Translated by liyifeng

题目描述

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.

输入输出格式

输入格式:

The first input line contains integer n ( 1<=n<=2·10^6^ ) — length of the text. The following line contains nnlower-case Latin letters (from a to z).

输出格式:

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

输入输出样例

输入样例#1: 复制

4
babb

输出样例#1: 复制

6

输入样例#2: 复制

2
aa

输出样例#2: 复制

2


题解

一道比较不错的题目。
解法和P1872 回文串计数(回文树)差不多。
我们就是先统计一遍不会相交的情况,在统计一遍所有情况。
把所有情况减去不会相交的情况就可以了。
简单吗,简单。
一交代码
然后,你就会发现,你RE了。
让我们看一手数据。
1<=n<=2·10^6^
空间复杂度铁定GG。出题人心机婊
所以我们用邻接表来代替trie的直接储存。
以时间换空间。Orz。


代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll tot,sum,num;
ll n,ans,p1[2000001],p2[2000001];
int head[2000001];
struct node{
    int fail,len,cnt,dep;
}t[2000001];
struct nod{
    int next,to,v;
}e[2000001];
char s[2000001];
const int mod=51123987;
int read()
{
    int x=0,w=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}

void link(int from,int to,int v){
    num++;
    e[num].to=to;
    e[num].v=v;
    e[num].next=head[from];
    head[from]=num;
}

int tr(int x,int v)
{
    for(int i=head[x];i;i=e[i].next)
    if(e[i].v==v)return e[i].to;
    return 0;
}

void solve1()
{
    int len=strlen(s+1),k=0;s[0]='#';
    t[0].fail=t[1].fail=1;t[1].len=-1;tot=1;
    for(int i=1;i<=len;i++)
    {
        while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
        if(!tr(k,s[i]-'a')){
            t[++tot].len=t[k].len+2;
            int j=t[k].fail;
            while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
            t[tot].fail=tr(j,s[i]-'a');
            t[tot].dep=(t[t[tot].fail].dep+1)%mod;
            link(k,tot,s[i]-'a');
        }
        k=tr(k,s[i]-'a');
        p1[i]=(t[k].dep)%mod;
        t[k].cnt++;
    }
    for(int i=tot;i>=2;i--)
    t[t[i].fail].cnt+=t[i].cnt;
    for(int i=tot;i>=2;i--)
    if(t[i].len!=1)
    sum+=t[i].cnt;
}

void solve2()
{
    int len=strlen(s+1),k=0;s[0]='#';
    t[0].fail=t[1].fail=1;t[1].len=-1;tot=1;
    for(int i=1;i<=len;i++)
    {
        while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
        if(!tr(k,s[i]-'a')){
            t[++tot].len=t[k].len+2;
            int j=t[k].fail;
            while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
            t[tot].fail=tr(j,s[i]-'a');
            t[tot].dep=(t[t[tot].fail].dep+1)%mod;
            link(k,tot,s[i]-'a');
        }
        k=tr(k,s[i]-'a');
        t[k].cnt++;
        p2[len-i+1]=(t[k].dep)%mod;
    }
}

int main()
{
    n=read();
    scanf("%s",s+1);
    int len=strlen(s+1);
    solve1();
    reverse(s+1,s+len+1);
    memset(t,0,sizeof(t));
    num=0;
    memset(head,0,sizeof(head));
    solve2();
    sum%=mod;n%=mod;
    for(int i=1;i<=len;i++)p1[i]+=p1[i-1],p1[i]%=mod;
    for(int i=1;i<=len;i++)ans+=(p1[i]*p2[i+1])%mod,ans%=mod;
    printf("%lld",(ll)((((sum+n)*(sum+n-1))/2%mod-ans)+mod)%mod);
    return 0;
}

原文地址:https://www.cnblogs.com/hhh1109/p/9246070.html

时间: 2024-11-02 07:26:42

CF17E Palisection(回文树)的相关文章

CodeForces 17E 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 at all, and remembered about wonderful strings called palindromes. We should remin

【CF17E】Palisection(回文树)

[CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文串的数量 然后对应的乘一下就行了 求法我用的是回文树 维护每个节点到根节点的距离, 就是回文后缀的数量 CF上的空间是\(128MB\) 卡的很 所以所有的连边考虑用邻接表来代替 #include<iostream> #include<cstdio> #include<cstdl

回文树

(没有坑怎么填?) 最近膜了一些关于回文串的题目,感到非常有意思,遂开篇记录. 在逛UOJ的题目时发现了vfk添上了新题,APIO 2014的题目.本身是一件很正常的事,而它事实上也没有变成什么了不得的事.我看到了Palindrome这个标题---回文串已经烂大街了,没什么新意.不过我很早就向学习回文树这东西了,久仰其大名而未尝真正去了结果它,于是我就顺手撸了一把豪哥的论文,发现他讲解的实在是晦涩难懂---论文的通病,就是虽然表述没有歧义,但是难以理解.嘛,然后我就找了几个标程,发现回文树这东西

回文树或者回文自动机,及相关例题

回文树简述 在大部分说法中,回文树与回文自动机指的是一个东西: 回文树是对一个字符串,基于自动机思想构建的处理回文问题的树形结构: 回文树是对着一个单串建立的: 于是他主要用于计数(回文子串种类及个数) 基本建立思路是先建立其前缀的回文树,然后每加上一个字符,统计影响: 回文树存在fail指针但一般不承接字符串匹配问题: (回文树大概可以判定一个回文串是不是一个串的子串,但KMP之类的可以做得更好) 构建好的回文树,是这样的: (好难看) 可看出: 存在两个树结构,分别记录奇数|偶数长度的回文:

bzoj3676: [Apio2014]回文串 回文树

回文树的裸题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=500100; const int INF=1e9+10; struct PalinTree { int ch[maxn][26],f[maxn]; int

HDU3948 &amp; 回文树模板

Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Created time: 2016-04-20 20:55 # Filename: hdu3948.cpp # Description: =================================*/ #define me AcrossTheSky&HalfSummer11 #include &l

hdu5658 CA Loves Palindromic 回文树

回文树在处理回文方面真的比manacher要好用得多... #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; t

HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 223    Accepted Submission(s): 110 Problem Description Harry got a string T, he wanted to know the number of T's disjoint

BZOJ 3676 [Apio2014]回文串(回文树)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3676 [题目大意] 考虑一个只包含小写拉丁字母的字符串s. 我们定义s的一个子串t的"出现值"为t在s中的出现次数乘以t的长度. 求s的所有回文子串中的最大出现值. [题解] 我们对给出串建立回文树,统计每个回文串出现次数和长度,相乘取组大即可 [代码] #include <cstdio> #include <algorithm> #include