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;
}

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

  1. Ural 2040. Palindromes and Super Abilities 2 回文自动机

    2040. Palindromes and Super Abilities 2 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2040 ...

  2. URAL 2040 Palindromes and Super Abilities 2 (回文自动机)

    Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...

  3. URAL 2040 Palindromes and Super Abilities 2

    Palindromes and Super Abilities 2Time Limit: 500MS Memory Limit: 102400KB 64bit IO Format: %I64d &am ...

  4. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

  5. Ural 1960 Palindromes and Super Abilities

    Palindromes and Super Abilities Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged ...

  6. 回文树练习 Part1

    URAL - 1960   Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...

  7. 【URAL】1960. Palindromes and Super Abilities

    http://acm.timus.ru/problem.aspx?space=1&num=1960 题意:给一个串s,要求输出所有的s[0]~s[i],i<|s|的回文串数目.(|s|& ...

  8. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

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

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

随机推荐

  1. 利用python进行泰坦尼克生存预测——数据探索分析

    最近一直断断续续的做这个泰坦尼克生存预测模型的练习,这个kaggle的竞赛题,网上有很多人都分享过,而且都很成熟,也有些写的非常详细,我主要是在牛人们的基础上,按照数据挖掘流程梳理思路,然后通过练习每 ...

  2. python --闭包学习

    闭包概念: Closure:内部函数中对enclosing作用域的变量进行引用 enclosing作用域:函数内部与内嵌函之间 范例1: #coding:utf-8 def set_passline( ...

  3. SQL Server 保存不了修改后的表的解决方法

    客户端报错(配图): 出现问题环境:在新建一个表之后,又想添加一个字段保存表,提示错误. 解决方案:在SQL Server 2008R2 中 对于对于重新保存新建表系统默认设置为“阻止”,在对应设置解 ...

  4. python字符串操作,以及对应的C#实现

    --IndexOf-- python: inx = str.find("aa") c#: var inx = str.IndexOf("aa"); --Last ...

  5. 171. Anagrams【medium】

    Given an array of strings, return all groups of strings that are anagrams. Notice All inputs will be ...

  6. 创建一个很大的EMP表 EMP_LARGE

    --CREATE TABLE EMP_LARGE AS SELECT * FROM EMP ; ---先复制一张EMP表 DECLARE --声明变量 v_loop NUMBER; v_num NUM ...

  7. CSS学习笔记(2)--html中checkbox和radio

    checkbox复选,radio单选 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  8. linux学习笔记命令篇1---命令ls

    前言:  linux中接触最多的就是命令和文件. 命令 命令是有其格式的, 一般格式是command [option] parameter1 parameter2 [paramete3 ...]: 注 ...

  9. Python Excel 导入导出【转】

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...

  10. Hbase存储详解

    转自:http://my.oschina.net/mkh/blog/349866 Hbase存储详解 started by chad walters and jim 2006.11 G release ...