您的位置:首页 > 其它

URAL1960 Palindromes and Super Abilities

2017-02-17 18:29 393 查看
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

inputoutput
aba
1 2 3

 

按顺序每次添加一个字符,求当前本质不同的回文串的数量

 

回文自动机

刚开始没意识到“本质不同”,加了个统计出现次数……

/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=102100;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char s[mxn];
struct Pam{
int t[mxn][27];
int l[mxn],sz[mxn],fa[mxn];
int res[mxn];
int S,cnt,last;
void init(){S=cnt=last=fa[0]=fa[1]=1;l[1]=-1;return;}
void add(int c,int n){
int p=last;
while(s[n-l

-1]!=s )p=fa[p]; if(!t[p][c]){ int np=++cnt;l[np]=l[p]+2; int k=fa[p]; while(s[n-l[k]-1]!=s )k=fa[k]; fa[np]=t[k][c];//fail指针 t[p][c]=np; } last=t[p][c]; sz[last]++;//统计出现次数 } void solve(){ printf("%d ",cnt-1); /* 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+1); int len=strlen(s+1); hw.init(); for(i=1;i<=len;i++){ hw.add(s[i]-'a',i); hw.solve(); } return 0; }

[p] 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: