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. nyoj-257 郁闷的C小加(一) 前缀表达式变后缀

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...

  2. Android Handler 避免内存泄漏的用法总结

    Android开发经常会用到handler,但是我们发现每次使用Handler都会出现:This Handler class should be static or leaks might occur ...

  3. ASP.NET Web API上实现 Web Socket

    1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...

  4. sdut 2831 Euclid (几何)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831 题意:给a, b,  c,  d, ...

  5. sqlserver得到昨天的数据

    SELECT * FROM test where DATEDIFF(d,witdate,getdate()) = 1 witdate表示的比较字段

  6. table share

    每个表的表结构会放到table_def_cache中,一个table share对应一个实例 table share 又会实例化为一个对象, 每个进程,每个实例化的对象,

  7. 【笨嘴拙舌WINDOWS】编码历史

    在介绍历史之前,有必要将一个经常使用的词语"标准"解释一下: " 标准是"为了在一定的范围内获得最佳秩序,经协商一致制定并由公认机构批准,共同使用的和重复使用的 ...

  8. jquery dialog-优雅的弹出框

    前面一章已经对datepicker的使用,做了简单的说明.这一章主要对dialog如何使用做个说明.         jquery ui-dialog在web开发中运用还是比较多的.最常见的例子就是登 ...

  9. HNOI2008越狱(快速幂)

    快速幂水过,贴一下模版. ; var x,y,n,m:int64; function power(num,times:int64):int64; var temp:int64; begin then ...

  10. 零基础学通C语言,福利来啦!!!!zfhl.ke.qq.com