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;

}

最新文章

  1. 初学者对于MVC架构模式学习与理解
  2. C中的字符串实例
  3. SQL 数据结构操作语句
  4. 安全增强 Linux (SELinux) 剖析
  5. linxu fcntl 函数用法 【转】
  6. VS发布网站步骤(先在vs上发布网站到新的文件夹,然后挂到iis上面)
  7. 时间戳 获得当前时间 -iOS
  8. 编程零基础应当如何开始学习 Python?
  9. Asp.net mvc 知多少(五)
  10. 执行Runtime.exec()需要注意的陷阱
  11. H5新特性-视频,音频-Flash-canvas绘图
  12. js实现60s倒计时效果
  13. Java第一次作业——Java语言基础
  14. introsort(内省排序)
  15. 【动态规划】最大子段和问题,最大子矩阵和问题,最大m子段和问题
  16. javascript读取xml文件读取节点数据的例子
  17. React Native vs. Cordova.
  18. Linux基础之-shell script(变量,运算符,流程控制,函数)
  19. Django模板语言之组合搜索
  20. 公网通过代理访问阿里云vpc redis

热门文章

  1. Drawable(5)关于从资源文件构造的Drawable不显示
  2. Android测试框架-uiautomator
  3. How to install JDK (Java Development Kit) on Linux
  4. poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)
  5. 函数rec_get_nth_field_offs_old
  6. uva 10054 The Necklac(欧拉回路)
  7. I.MX6 lcd lvds hdmi bootargs
  8. POJ 1523 SPF (割点,连通分量)
  9. wifi详解(三)
  10. IO负载高的来源定位