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

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 n numbers 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 prefix s1 … si−1. The first number in the output should be one.
 
Sample Input
abbbba
 
Sample Output
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) 
 
解题:PalindromicTree
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct PalindromicTree{
struct node{
int son[],f,len;
void init(int len){
memset(son,,sizeof son);
this->len = len;
}
}e[maxn];
int tot,last,n;
char s[maxn];
int newnode(int len = ){
e[tot].init(len);
return tot++;
}
int getFail(int x){
while(s[n - e[x].len - ] != s[n]) x = e[x].f;
return x;
}
void init(){
last = tot = n = ;
newnode();
newnode(-);
e[].f = ;
s[n] = -;
}
bool extend(int c){
s[++n] = c;
int cur = getFail(last);
bool isExtend = e[cur].son[c] == ;
if(!e[cur].son[c]){
int newson = newnode(e[cur].len + );
e[newson].f = e[getFail(e[cur].f)].son[c];
e[cur].son[c] = newson;
}
last = e[cur].son[c];
return isExtend;
}
}pt;
char str[maxn];
int main(){
while(gets(str)){
pt.init();
for(int i = ; str[i]; ++i){
if(pt.extend(str[i] - 'a')) putchar('');
else putchar('');
}
putchar('\n');
}
return ;
}
 

最新文章

  1. Js: Extensible Calendar Examples
  2. WPF依赖属性详解
  3. 解决Android时时更新listview数组越界问题
  4. Java 性能要点:自动装箱/ 拆箱 (Autoboxing / Unboxing)
  5. Protel对话窗字体显示不完全问题解决办法(PCB)
  6. drupal中使用drush命令,快速批量的开启和关闭模块
  7. [M]表格中的天正文字转换问题
  8. linux 安装 cenos7 和 jdk
  9. Mybatis-----优化配置文件,基于注解CR
  10. springcloud之hystrix熔断器-Finchley.SR2版
  11. __x__(12)0906第三天__&lt;meta&gt;标签
  12. 阿里云ECS Centos7 系统安装图形化桌面
  13. 墨者学院——密码学加解密实训(Base64转义)
  14. 「HNOI2016」网络 解题报告
  15. USB协议枚举过程详解
  16. (转)Redis集群搭建与简单使用(最少需要 6个节点)
  17. Hadoop YARN上运行MapReduce程序
  18. 命令行下的html转pdf工具wkhtmltopdf
  19. Kafka(一)-- 初体验
  20. Unity中Surface Shader执行过程

热门文章

  1. SpringMVC之基于注解的Controller
  2. CAS分析
  3. JDK使用最多的模式之一--观察者模式
  4. 事件对象(示例、封装函数EventUtil())
  5. Vue 2.0入门基础知识之内部指令
  6. Android手机屏幕投射到电脑神器Vysor
  7. Python学习日记之Python函数及方法使用总结
  8. JavaScript 的垃圾回收与内存泄露
  9. 跟随鼠标指针跑的div拖拽效果
  10. python基础一 day2 字符串操作