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. Solr3.6.2和Solr4.9.0经常使用配置

    tomcat 以tomcat 7为例,位置/work/apache-tomcat-7.0.55 Solr 3.6.2 基本配置 Solr 3.6.2.须要JDK 6/JDK7支持. 下载Solr 3. ...

  2. atitit.项目设计模式---ioc attilax总结

    atitit.项目设计模式---ioc attilax总结 1. .IOC的之前 1 2. ioc后的实现 1 3. 认识引入IOC框架的缺点, 2 4. 自己实现ioc 3 4.1. ioc框架的实 ...

  3. atitit.系统托盘图标的设计java swing c# .net c++ js

    atitit.系统托盘图标的实现java swing c# .net c++ js 1. 系统托盘图标的结构 1 2. Java swing的实现 1 3. .net的实现 1 4. C++的实现 1 ...

  4. 电脑不识别USB blaster驱动问题

    电脑不识别USB blaster,如下图: 解决办法:手动更新 http://zhidao.baidu.com/link?url=snVT__AsbtmQ4U5EBVN05Yrgv1TPv7AdVYe ...

  5. strncmp----c++库函数

    某天,蒜头君和花椰妹在公园里散步,走着走着,我的天!他们各自都捡到了一串漂亮的字符串,然而蒜头君好奇心比较重,他想知道自己的字符串在花椰妹的字符串中出现了多少次,例如花椰妹的字符串为abababa,蒜 ...

  6. DelphiXE8怎么使用调试模式(小米手机2)

    需求:在开发Android程序时,大家一直是使用ShowMessage.其实XE是支持下断点的. 操作:小米手机2: 1.小米手机2用USB线,连到电脑上. 2.小米手机2-设置-关于手机-" ...

  7. 跟着百度学PHP[17]-复习PDO的几个关键词

    主要就是复习一下几个关键词.发现太久没有写又忘了.惭愧. final self static const

  8. uGUI Anchor

    Anchor定位:inspector面板的Rect Transform组件中PosX左边的方框图标就是设置锚点的,做界面自适应时可定位控件在视图中的位置,与NGUI类似.Anchor+Canvas的C ...

  9. Unix系统编程()open,read,write和lseek的综合练习

    需求:程序的第一个命令行参数为将要打开的文件名称,余下的参数则指定了文件上执行的输入输出操作.每个表示操作的参数都以一个字母开头,紧跟以相关值(中间无空格分隔). soffet:从文件开始检索到off ...

  10. VmWare下安装CentOS6图文安装教程

    本教程使用的liunx系统(CentOS6)下载地址:http://download.csdn.net/download/yichen01010/10016202 一 先安装VMware. 双击安装文 ...