【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

题面

洛谷
求一个串中包含几个回文串

题解

Manacher傻逼题
只是用回文树写写而已。。

#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 10000
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;
}
int n;
char s[MAX];
int size[MAX];
struct PT
{
    struct Node
    {
        int son[26];
        int ff,len;
    }t[MAX];
    int last,tot;
    void init()
    {
        t[tot=1].len=-1;
        t[0].ff=t[1].ff=1;
    }
    void extend(int c,int n,char *s)
    {
        int p=last;
        while(s[n-t[p].len-1]!=s[n])p=t[p].ff;
        if(!t[p].son[c])
        {
            int v=++tot,k=t[p].ff;
            while(s[n-t[k].len-1]!=s[n])k=t[k].ff;
            t[v].len=t[p].len+2;
            t[v].ff=t[k].son[c];
            t[p].son[c]=v;
        }
        ++size[last=t[p].son[c]];
    }
    void Calc()
    {
        for(int i=tot;i;--i)size[t[i].ff]+=size[i];
    }
}PT;
int ans;
int main()
{
    PT.init();
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;++i)PT.extend(s[i]-97,i,s);
    PT.Calc();
    for(int i=2;i<=PT.tot;++i)ans+=size[i];
    printf("%d\n",ans);
    return 0;
}

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

时间: 2024-10-13 23:27:19

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)的相关文章

CF245H Queries for Number of Palindromes(回文树)

题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q(1<=q<=10^6) 第2至2+q行 给出每组询问的l和r 输出格式 输出每组询问所问的数量. 题目描述 You've got a string s=\(s_{1}\)\(s_{2}\)...\(s_{|s|}\) of length |s| , consisting of lowercase

URAL 2040 Palindromes and Super Abilities 2(回文树)

Palindromes and Super Abilities 2 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d & %I64u Status Description Dima adds letters s1, -, sn one by one to the end of a word. After each letter, he asks Misha to tell him how many new pali

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

回文树

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

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

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

HDU 5658 CA Loves Palindromic(回文树)

CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 301    Accepted Submission(s): 131 Problem Description CA loves strings, especially loves the palindrome strings. One day

ZOJ 3661 Palindromic Substring(回文树)

Palindromic Substring Time Limit: 10 Seconds      Memory Limit: 65536 KB In the kingdom of string, people like palindromic strings very much. They like only palindromic strings and dislike all other strings. There is a unified formula to calculate th

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 sh