天天看點

URAL 1960 Palindromes and Super Abilities

Description

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

ithat are palindromes.

Sample Input

input output
aba 1 2 3
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int T, n;
char s[maxn];

struct PalindromicTree
{
  const static int maxn = 1e5 + 10;
  const static int size = 26;
  int next[maxn][size], last;
  int fail[maxn], len[maxn], sz, tot;
  char s[maxn];
  void clear() 
  { 
    len[1] = -1; len[2] = 0;
    fail[2] = fail[1] = 1;  
    last = (sz = 3) - 1;  tot = 0;
    memset(next[1], 0, sizeof(next[1]));
    memset(next[2], 0, sizeof(next[2]));
  }
  int add(char pos)
  {
    int x = (s[++tot] = pos) - 'a';
    int y = last, z = len[last];
    while (true)
    {
      if (tot - 1 - z > 0 && pos == s[tot - 1 - z]) break;
      y = fail[y];  z = len[y];
    }
    if (next[y][x]) { last = next[y][x]; return 0; }

    last = next[y][x] = sz;
    len[sz] = len[y] + 2;
    memset(next[sz], 0, sizeof(next[sz]));
    if (len[sz] == 1) { fail[sz++] = 2; return 1; }

    while (true)
    {
      y = fail[y];  z = len[y];
      if (tot - 1 - z > 0 && pos == s[tot - 1 - z]) break;
    }
    fail[sz++] = next[y][x];
    return 1;
  }
}solve;

int main()
{
  while (scanf("%s", s) != EOF)
  {
    solve.clear();
    int ans = 0;
    for (int i = 0; s[i]; i++)
    {
      ans += solve.add(s[i]);
      printf("%s%d", i ? " " : "", ans);
    }
    putchar(10);
  }
  return 0;
}