Codeforces 557E Ann and Half-Palindrome (Trie树)

来源:互联网 发布:宁武子 邦有道则知读音 编辑:程序博客网 时间:2024/05/01 06:42

题目大意:

就是现在给出长度不超过5000的只包含小写字母'a'和‘b'的字符串

定义半回文串:字符串S是半回文串的条件是S[i] = S[|S| - i + 1] 对所有的计数 i, i <= |S|成立

然后给出整数K, 求给出的字符串中所有的半回文串中字典序第K小的是什么

相同样子但是出现位置不同视为不同


大致思路:

首先可以O(|S|^2)处理出所有的半回文串, 用half[u][v]表示子串S[u, v]是否是半回文串

然后对于这个字符串将所有的后缀串插入到一个Trie树中, 在每个结点处增加一个域表示这个结点代表的半回文串出现的次数, Trie树建立的复杂度是O(2*|S|*|S|), 2是字符集大小的原因

那么在Trie树上做一个树上的DP统计出每个节点为根的子树下有多少个半回文串

然后对于询问K直接在树上递归向下就可以找出答案


代码如下:

Result  :  Accepted     Memory  :  220444 KB     Time  :  311 ms

/* * Author: Gatevin * Created Time:  2015/10/1 13:59:05 * File Name: A.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;bool half[5010][5010];int K;int len;char s[5010];struct Trie{    int next[12500010][2];    int end[12500010];    int cnt[12500010];    int L, root;        int newnode()    {        next[L][0] = next[L][1] = -1;        end[L++] = 0;        return L - 1;    }        void init()    {        L = 0;        root = newnode();    }        void insert(int u, int v)    {        int now = root;        for(int i = u; i <= v; i++)        {            if(next[now][s[i] - 'a'] == -1)                next[now][s[i] - 'a'] = newnode();            now = next[now][s[i] - 'a'];            end[now] += half[u][i];        }        return;    }        int dfs(int now)    {        cnt[now] = end[now];        for(int i = 0; i < 2; i++)            if(next[now][i] != -1)                cnt[now] += dfs(next[now][i]);        return cnt[now];    }        void find(int now, int K)    {        if(K <= end[now])            return;        if(next[now][0] != -1 && K - end[now] <= cnt[next[now][0]])        {            putchar('a');            find(next[now][0], K - end[now]);        }        else        {            putchar('b');            if(next[now][0] != -1)                find(next[now][1], K - end[now] - cnt[next[now][0]]);            else find(next[now][1], K - end[now]);        }        return;    }        void solve()    {        dfs(root);        find(root, K);    }};Trie trie;int main(){    scanf("%s", s);    scanf("%d", &K);    len = strlen(s);    bool ok1, ok2;    for(int i = 0; i < len; i++)    {        ok1 = ok2 = 1;        for(int r = 0; i - r >= 0 && i + r < len; r++)        {            if(r & 1)            {                if(s[i - r] == s[i + r] && ok1)                    half[i - r][i + r] = 1;                else ok1 = 0;            }            else            {                if(s[i - r] == s[i + r] && ok2)                    half[i - r][i + r] = 1;                else ok2 = 0;            }        }    }    for(int l = 0, r = 1; r < len; l++, r++)    {        ok1 = ok2 = 1;        for(int R = 0; l - R >= 0 && r + R < len; R++)        {            if(R & 1)            {                if(s[l - R] == s[r + R] && ok1)                    half[l - R][r + R] = 1;                else ok1 = 0;            }            else            {                if(s[l - R] == s[r + R] && ok2)                    half[l - R][r + R] = 1;                else ok2 = 0;            }        }    }    trie.init();    for(int i = 0; i < len; i++)        trie.insert(i, len - 1);    trie.solve();    return 0;}


0 0