URAL1960 Palindromes and Super Abilities
2024-09-04 17:26:54
After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Misha has got an unusual ability. Now, when he reads a word, he can mentally count the number of unique nonempty substrings of this word that are palindromes.
Dima wants to test Misha’s new ability. He adds letters s 1, ..., s n to a word, letter by letter, and after every letter asks Misha, how many different nonempty palindromes current word contains as substrings. Which n numbers will Misha say, if he will never be wrong?
Input
The only line of input contains the string s 1... s n, where s i are small English letters (1 ≤ n ≤ 10 5).
Output
Output n numbers separated by whitespaces, i-th of these numbers must be the number of different nonempty substrings of prefix s 1... s i that are palindromes.
Example
input | output |
---|---|
aba |
1 2 3 |
按顺序每次添加一个字符,求当前本质不同的回文串的数量
回文自动机
刚开始没意识到“本质不同”,加了个统计出现次数……
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
char s[mxn];
struct Pam{
int t[mxn][];
int l[mxn],sz[mxn],fa[mxn];
int res[mxn];
int S,cnt,last;
void init(){S=cnt=last=fa[]=fa[]=;l[]=-;return;}
void add(int c,int n){
int p=last;
while(s[n-l[p]-]!=s[n])p=fa[p];
if(!t[p][c]){
int np=++cnt;l[np]=l[p]+;
int k=fa[p];
while(s[n-l[k]-]!=s[n])k=fa[k];
fa[np]=t[k][c];//fail指针
t[p][c]=np;
}
last=t[p][c];
sz[last]++;//统计出现次数
}
void solve(){
printf("%d ",cnt-);
/*
memset(res,0,sizeof res);
int ans=0;
for(int i=cnt;i;i--){
res[i]+=sz[i];
res[fa[i]]+=res[i];
ans+=res[i];
}
printf("%d ",ans);*/
return;
}
}hw;
int main(){
int i,j;
scanf("%s",s+);
int len=strlen(s+);
hw.init();
for(i=;i<=len;i++){
hw.add(s[i]-'a',i);
hw.solve();
}
return ;
}
最新文章
- 【codeforces 148D】 Bag of mice
- jasper 常用知识点总结
- MYSQL入门(三)
- MySQL基础 - 外键和约束
- C++ 类的静态成员详细讲解[转]
- 理解ATL中的一些汇编代码(通过Thunk技术来调用类成员函数)
- 基于Socket的UDP和TCP编程介绍
- An update on OS X Code Signing(OS X代码签名)
- hadoop - spark on yarn 集群搭建
- Django 后台定制自己的选择框删除函数
- postman连续添加多个订单&;jmeter快速审核添加订单
- IPerf——网络测试工具介绍与源码解析(1)
- phpcms首页替换
- Task使用
- 如何为你的树莓派安装一个WIN10系统?(非iot)
- python 全栈开发,Day58(bootstrap组件,bootstrap JavaScript 插件,后台模板,图表插件,jQuery插件库,Animate.css,swiper,运行vue项目)
- 【java编程】格式化字符串
- 查找被占用的端口的服务并kill掉
- java中使用OpenOffice
- GoLang 命令
热门文章
- scrapy 圣墟
- git bash学习3 -简单杂乱知识点记录
- 第五篇:selenium调用IE问题(Protected Mode settings are not the same for all zones)
- 怎样查看web软件例如apache的连接数
- 学习Pytbon第八天,文件的操作
- C++构造函数实例——拷贝构造,赋值
- A1058 A+B in Hogwarts (20)(20 分)
- 1497: [NOI2006]最大获利(最大权闭合子图)
- 非负矩阵分解(NMF)原理及算法实现
- RSA进阶之两个N的公约数