当前位置 : 主页 > 手机开发 > ROM >

Codeforces 557E Ann and Half-Palindrome

来源:互联网 收集:自由互联 发布时间:2021-06-10
Ann and Half-Palindrome 我居然写了个后缀排序。。 其实暴力把所有合法串丢进字典树里面就行了。 #includebits/stdc++.h #define LL long long #define LD long double #define ull unsigned long long #define fi first #

Ann and Half-Palindrome

我居然写了个后缀排序。。

其实暴力把所有合法串丢进字典树里面就行了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 5000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

char s[N << 1];
int n, k, id[N], rk[N], lcp[N][N];
bool dp[N][N];

int row[N][N], col[N][N];
int ansL, ansR;

void dfs(int l, int r, int be, int k) {
    if(l == r) {
        int p = id[l];
        for(int j = p + be - 1; j <= n; j++) {
            if(dp[p][j]) {
                k--;
                if(!k) {
                    ansL = p, ansR = j;
                    break;
                }
            }
        }
        return;
    }
    while(n - id[l] + 1 < be) {
        l++;
    }
    int bor = l;
    int tota = 0, havea = 0;
    int totb = 0, haveb = 0;
    for(int i = l; i <= r; i++) {
        if(s[id[i] + be - 1] == a) {
            tota += row[i][n] - row[i][be - 1];
            havea += dp[id[i]][id[i] + be - 1];
            bor++;
        } else {
            totb += row[i][n] - row[i][be - 1];
            haveb += dp[id[i]][id[i] + be - 1];
        }
    }
    if(bor == l) {
        if(haveb < k) dfs(l, r, be + 1, k - haveb);
        else {
            ansL = id[l];
            ansR = id[l] + be - 1;
            return;
        }
    } else if(bor == r + 1) {
        if(havea < k) dfs(l, r, be + 1, k - havea);
        else {
            ansL = id[l];
            ansR = id[l] + be - 1;
            return;
        }
    } else {
        if(tota >= k) dfs(l, bor - 1, be, k);
        else dfs(bor, r, be, k - tota);
    }
}

int main() {
    scanf("%s%d", s + 1, &k);
    n = strlen(s + 1);
    for(int i = n; i >= 1; i--) {
        for(int j = n; j > i; j--) {
            lcp[i][j] = s[i] == s[j] ? lcp[i + 1][j + 1] + 1 : 0;
            lcp[j][i] = lcp[i][j];
        }
    }
    for(int i = 1; i <= n; i++) id[i] = i;
    sort(id + 1, id + 1 + n, [&](int a, int b) {
        int len = lcp[a][b];
        return s[a + len] < s[b + len];
    });
    for(int i = 1; i <= n; i++) rk[id[i]] = i;
    for(int i = 1; i <= n; i++) dp[i][i] = true;
    for(int i = 1; i < n; i++) dp[i][i + 1] = s[i] == s[i + 1];
    for(int len = 3; len <= n; len++) {
        for(int i = 1; i + len - 1 <= n; i++) {
            int j = i + len - 1;
            if(len <= 4) dp[i][j] = s[i] == s[j];
            else if(len == 6) dp[i][j] = s[i] == s[j] && s[i + 2] == s[j - 2];
            else dp[i][j] = s[i] == s[j] && dp[i + 2][j - 2];

        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            row[rk[i]][j - i + 1] = dp[i][j];
            col[rk[i]][j - i + 1] = dp[i][j];
        }
    }

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            row[i][j] += row[i][j - 1];
            col[i][j] += col[i - 1][j];
        }
    }
    dfs(1, n, 1, k);
    for(int i = ansL; i <= ansR; i++) putchar(s[i]);
    puts("");
    return 0;
}

/*
*/
网友评论