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 palindrome substrings appeared when he added that letter.
Two substrings are considered distinct if they are different as strings. Which nnumbers will be said by Misha if it is known that he is never wrong?

Input

The input contains a string s1 … sn consisting of letters ‘a’ and ‘b’ (1 ≤ n ≤ 5 000 000).

Output

Print n numbers without spaces: i-th number must be the number of palindrome substrings of the prefix s1 … si minus the number of palindrome substrings of the
prefixs1 … si?1. The first number in the output should be one.

Sample Input

input output
abbbba
111111

Notes

We guarantee that jury has C++ solution which fits Time Limit at least two times. We do not guarantee that solution on other languages exists (even Java).

Source

Problem Author: Mikhail Rubinchik (prepared by Kirill Borozdin)

Problem Source: Ural FU Dandelion contest. Petrozavodsk training camp. Summer 2014

每插入一个字符都会有两种情况,产生新的回文树节点,不产生新的节点。所以答案只会是0,1

#include <iostream>
#include <string.h>

#include <stdlib.h>
#include <math.h>
#include <stdio.h>

using namespace std;
typedef long long int LL;
const int MAX=5*1e6;
const int maxn=4*1e6+5;
char str[MAX+5];
struct Tree
{
    int next[maxn][2];
    int fail[MAX+5];
    int len[MAX+5];
    int s[MAX+5];
    int last,n,p;
    int new_node(int x)
    {
        memset(next[p],0,sizeof(next[p]));
        len[p]=x;
        return p++;
    }
    void init()
    {
        p=0;
        new_node(0);
        new_node(-1);
        last=0;n=0;
        s[0]=-1;
        fail[0]=1;
	}
    int get_fail(int x)
    {
        while(s[n-len[x]-1]!=s[n])
            x=fail[x];
        return x;
    }
    int add(int x)
    {
        x-='a';
        s[++n]=x;
        int cur=get_fail(last);
        if(!(last=next[cur][x]))
        {
            int now=new_node(len[cur]+2);
            fail[now]=next[get_fail(fail[cur])][x];
            next[cur][x]=now;
            last=now;
            return 1;
        }
        return 0;
    }

}tree;
char ans[MAX+5];
int main()
{
    while(scanf("%s",str)!=EOF)
    {

        tree.init();
		int i;
        for( i=0;str[i];i++)
        {
			if(!tree.add(str[i]))
			    ans[i]='0';
			else
				ans[i]='1';
		}
		ans[i]='\0';
		puts(ans);
	}
    return 0;
}
时间: 2024-09-28 13:26:10

URAL 2040 Palindromes and Super Abilities 2(回文树)的相关文章

Ural 2040 Palindromes and Super Abilities 2

题目太坑,发一波纪念一下 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=5000010; 5 struct PAM{ 6 int cnt,last; 7 int a[N][2],l[N],f[N]; 8 char s[N]; 9 PAM(){ 10 cnt=f[0]=f[1]=1; 11 l[1]=-1; 12 } 13 int get_fail(int x,int

ural 1960 Palindromes and Super Abilities

题意:找出s[1..x]的本质不同的回文串的个数 分析:回文树模板题 /************************************************ Author :DarkTong Created Time :2016年09月21日 星期三 21时17分07秒 File Name :a.cpp *************************************************/ #include <bits/stdc++.h> using namespac

ural 1960 Palindromes and Super Abilities 题解

题意:给一个串s,按顺序一个个加入到序列里面.输出每次加入之后序列中的本质不同的回文串个数. 回文自动机模板题- -extend函数里面,如果那个if进去了,就代表多了一个本质不同的回文串. 1 #include<cstdio> 2 #include<cstring> 3 const int MAXN=100000+5; 4 const int SIGMA_SIZE=26; 5 struct Node{ 6 int num,len; 7 Node* go[SIGMA_SIZE],*

【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<

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

回文树

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

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

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(回文树)

题意翻译 给定一个长度为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