【HDU5421】Victor and String(回文树)

【HDU5421】Victor and String(回文树)

题面

Vjudge
大意:
你需要支持以下操作:
动态在前端插入一个字符
动态在后端插入一个字符
回答当前本质不同的回文串个数
回答当前回文串个数

题解

回文树前端插入的操作,学一学感觉并不难?
额外维护一下一个前端插入的\(last\)
然后就和后端插入一模一样,前端插入时就是前端插入的\(last\)
唯一会产生的影响的就是当前字符差完后,
发现现在的\(last\)就是整个串,那么就要更新另外一端的\(last\)为当前这一端的\(last\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 222222
inline int read()
{
    RG int x=0,t=1;RG char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}
struct Node
{
    int son[26],ff;
    int len,dep;
}t[MAX];
char s[MAX];
ll ans;
int tot,l,r,pre,suf,n;
void init()
{
    l=1e5,r=l-1;memset(s,'\0',sizeof(s));
    memset(t,0,sizeof(t));ans=0;
    t[pre=suf=0].ff=t[tot=1].ff=1;t[1].len=-1;
}
void extend(int c,int n,int &last,int op)
{
    int p=last;
    while(s[n]!=s[n-op*t[p].len-op])p=t[p].ff;
    if(!t[p].son[c])
    {
        int v=++tot,k=t[p].ff;t[v].len=t[p].len+2;
        while(s[n]!=s[n-op*t[k].len-op])k=t[k].ff;
        t[v].ff=t[k].son[c];t[p].son[c]=v;
        t[v].dep=t[t[v].ff].dep+1;
    }
    last=t[p].son[c];ans+=t[last].dep;
    if(t[last].len==r-l+1)pre=suf=last;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        while(n--)
        {
            int opt=read();
            if(opt<=2)
            {
                char c=getchar();
                if(opt==1)s[--l]=c,extend(c-97,l,pre,-1);
                else s[++r]=c,extend(c-97,r,suf,1);
            }
            else if(opt==3)printf("%d\n",tot-1);
            else printf("%lld\n",ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cjyyb/p/9210996.html

时间: 2024-07-30 03:06:45

【HDU5421】Victor and String(回文树)的相关文章

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

HDU 6599 I Love Palindrome String (回文树+hash)

题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所以该子串的前半部分和后半部分是本质相同的! 于是这个log就去掉了 代码 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring>

HDOJ 5421 Victor and String 回文串自己主动机

假设没有操作1,就是裸的回文串自己主动机...... 能够从头部插入字符的回文串自己主动机,维护两个last点就好了..... 当整个串都是回文串的时候把两个last统一一下 Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Others) Total Submission(s): 30    Accepted Submission(s): 9 Probl

HDU - 5421:Victor and String (回文树,支持首尾插入新字符)

Sample Input 6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4 Sample Output 4 5 4 5 11 题意:多组输入,开始字符串为空,支持4中操作: 1,在字符串首加字符: 2,在字符串尾加字符: 3,查询字符串不同本质的回文串个数: 4,查询回文串个数总和 思路:因为支持首尾加入,所以和常规的回文树有些不同. 参考了YYB的博客. 发现首尾互相影响,当且仅当整个字符串是回文串. 其他情况两头正常加即可. #include

HDU - 5157 :Harry and magic string (回文树)

Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置结尾的回文串数,那么累加得到前缀和: 倒着再做一遍得到每个位置为开头的回文串数,乘正向求出的前缀和即可. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define

HDU3948 &amp; 回文树模板

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

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

HYSBZ 3676 回文串 (回文树)

3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 1680  Solved: 707 [Submit][Status][Discuss] Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的"出 现值"为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行,为一个只包含小写字母(a -z)的非空字符串s. Output

HYSBZ 2565 最长双回文串 (回文树)

2565: 最长双回文串 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1377  Solved: 714 [Submit][Status][Discuss] Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同). 输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y