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

題目大意:

就是現在給出長度不超過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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章