前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >URAL 2040 Palindromes and Super Abilities 2(回文树)

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

作者头像
ShenduCC
发布2018-04-26 17:27:06
6520
发布2018-04-26 17:27:06
举报
文章被收录于专栏:算法修养

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

Source

Problem Author: Mikhail Rubinchik (prepared by Kirill Borozdin)  Problem Source: Ural FU Dandelion contest. Petrozavodsk training camp. Summer 2014 

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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-05-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档