URAL 1960 Palindromes and Super Abilities (Palindromic Tree)

题目大意:

就是对于给出的字符串s (长度不超过10^5), 求出其各个前缀含有的不同回文串的数量


大致思路:

其实就是Palindromic Tree的裸题...看懂了Palindromic Tree的结构之后这个简直不能再明显, 当做联系Palindromic Tree来做了,


代码如下:

Result  :  Accepted     Memory  :  12222 KB     Time  :  187 ms

/*
 * Author: Gatevin
 * Created Time:  2015/3/30 20:24:10
 * File Name: Rin_Tohsaka.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 100010
char s[maxn];

struct Palindromic_Tree
{
    struct node
    {
        int next[26];
        int len, sufflink;
        lint cnt;
    };
    node tree[maxn];
    int L, len, suff;
    void newnode()
    {
        L++;
        for(int i = 0; i < 26; i++)
            tree[L].next[i] = -1;
        tree[L].len = tree[L].sufflink = tree[L].cnt = 0;
        return;
    }
    void init()
    {
        L = 0, suff = 2;
        newnode(), newnode();
        tree[1].len = -1, tree[1].sufflink = 1;
        tree[2].len = 0, tree[2].sufflink = 1;
        return;
    }
    bool addLetter(int pos)
    {
        int cur = suff, curlen = 0;
        int alp = s[pos] - 'a';
        while(1)
        {
            curlen = tree[cur].len;
            if(pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])
                break;
            cur = tree[cur].sufflink;
        }
        if(tree[cur].next[alp] != -1)
        {
            suff = tree[cur].next[alp];
            return false;
        }
        newnode();
        suff = L;
        tree[L].len = tree[cur].len + 2;
        tree[cur].next[alp] = L;
        if(tree[L].len == 1)
        {
            tree[L].sufflink = 2;
            tree[L].cnt = 1;
            return true;
        }
        while(1)
        {
            cur = tree[cur].sufflink;
            curlen = tree[cur].len;
            if(pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])
            {
                tree[L].sufflink = tree[cur].next[alp];
                break;
            }
        }
        tree[L].cnt = 1 + tree[tree[L].sufflink].cnt;
        return true;
    }
};

Palindromic_Tree pal;

int main()
{
    scanf("%s", s);
    pal.init();
    int len = strlen(s);
    lint ans = 0;
    for(int i = 0; i < len; i++)
        printf("%I64d ", ans += pal.addLetter(i));
    return 0;
}


你可能感兴趣的:(tree,Palindromic,ural,1960)