您的位置:首页 > 其它

URAL2040-Palindromes and Super Abilities 2

2017-10-10 19:54 405 查看


Palindromes and Super Abilities 2

Time limit: 0.5 second

Memory limit: 100 MB

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

inputoutput
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).

Problem Author: Mikhail Rubinchik (prepared by Kirill Borozdin)
Problem Source: Ural FU Dandelion contest. Petrozavodsk training camp. Summer 2014

题意:给你一个字符串,按字符串的顺序每次增加一个字符的时候,如果增加了回文串种类,那就输出1,否则输出0

解题思路:回文树,但是会卡输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

const int maxn = 5e6 + 10;
char s[maxn], ans[maxn];

struct PalindromicTree
{
const static int maxn = 5e6 + 10;
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) - 1;
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;
}
}solve;

int main()
{
while (~scanf("%s", s))
{
solve.Clear();
int len = strlen(s);
for (int i = 0; i < len; i++) ans[i] = '0' + solve.add(s[i]);
ans[len] = '\0';
printf("%s\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: