Palindromes and Super Abilities 2

题目链接:

http://acm.hust.edu.cn/vjudge/contest/126823#problem/E

Description


Dima adds letters s 1, …, s n 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 n numbers will be said by Misha if it is known that he is never wrong?

Input


The input contains a string s 1 … s n 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 s 1 … s i minus the number of palindrome substrings of the prefix s 1 … s i−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).


##题意:

给出n个字符,要求依次输出填上第i个字符后不同的回文子串的个数增加了多少.


##题解:

可以推导出每次加一个字符,不同回文子串最多增加1个. 后面就没有然后了....

神奇的回文自动机.
先抄个板,后面学习.
这个题也是蛮严格的,卡内存+卡时间+卡输出.(只能用puts)


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 5001000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct PalindromicTree

{

int next[maxn][2], last, sz, tot;

int fail[maxn], len[maxn];

char s[maxn];

void clear()

{

len[1] = -1; len[2] = 0;

fail[2] = fail[1] = 1;

last = (sz = 3) - 2;

tot = 0;

memset(next[1], 0, sizeof(next[1]));

memset(next[2], 0, sizeof(next[2]));

}

int Node(int length)

{

memset(next[sz], 0, sizeof(next[sz]));

len[sz] = length; return sz++;

}

int getfail(int x)

{

while (s[tot] != s[tot - len[x] - 1]) x = fail[x];

return x;

}

int add(char pos)

{

int x = (s[++tot] = pos) - 'a', y = getfail(last);

if (next[y][x]) { last = next[y][x]; return 0; }

    last = next[y][x] = Node(len[y] + 2);
fail[last] = len[last] == 1 ? 2 : next[getfail(fail[y])][x];
return 1;
}

}T;

char Ans[maxn];

char str[maxn];

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%s", str) != EOF)
{
T.clear();
int len = strlen(str);
for(int i=0; i<len; i++) {
Ans[i] = T.add(str[i]) + '0';
}
Ans[len] = 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 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d ...

  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. 【URAL】1960. Palindromes and Super Abilities

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

  7. URAL 2040 (回文自动机)

    Problem Palindromes and Super Abilities 2 (URAL2040) 题目大意 给一个字符串,从左到右依次添加,询问每添加一个字符,新增加的回文串数量. 解题分析 ...

  8. URAL1960 Palindromes and Super Abilities

    After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Mis ...

  9. 【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习

    写题遇上一棘手的题,[Apio2014]回文串,一眼看过后缀数组+Manacher.然后就码码码...过是过了,然后看一下[Status],怎么慢这么多,不服..然后就搜了一下,发现一种新东西——回文 ...

随机推荐

  1. Android的底层库libutils介绍

    第一部分 libutils概述 libutils是Android的底层库,这个库以C++实现,它提供的API也是C++的.Android的层次的C语言程序和库,大都基于libutils开发. libu ...

  2. Eclipse中Python插件PyDev的安装与配置流程

    安装PyDev插件的两种安装方法: 方法1.下载地址:http://sourceforge.net/projects/pydev/files/,将下载的PyDev解压(目前最新版本 PyDev 4.5 ...

  3. Awesome Javascript(中文翻译版)

    [导读]:GitHub 上有一个 Awesome – XXX 系列的资源整理.awesome-javascript 是 sorrycc 发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架 ...

  4. poj 1703 Find them, Catch them(并查集)

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  5. android截屏:保存一个view的内容为图片并存放到SD卡

    项目中偶尔会用到截屏分享,于是就有了下面这个截屏的方法~ 下面得saveImage()方法就是保存当前Activity对应的屏幕所有内容的截屏保存. private void saveImage() ...

  6. 函数mem_area_alloc

    /********************************************************************//** Allocates memory from a po ...

  7. ArrayList和List之间的转换

    开发中不免碰到List与数组类型之间的相互转换,举一个简单的例子: package test.test1; import java.util.ArrayList; import java.util.L ...

  8. JAVA将Excel中的报表导出为图片格式(二)实现思路

    接上文,一封类似于下方设计的Excel报表,如何将它指定的区域导出为样式一模一样的JPG图片呢? 要实现这个功能没有现成的解决方案,谷歌度娘了好久也没有,最终自己想了几条思路: 思路1:将报表中的背景 ...

  9. 发布 windows 10 universal app 时微软账号验证失败

    具体错误:Visual Studio encountered an unexpected network error and can't contact the Microsoft account s ...

  10. GridView CommandArgument 绑定多个参数

    我们在使用GridView的时候 有时会需要绑定多个参数 <asp:GridView ID="gvwVoxListAll" runat="server"  ...