[Codeforces 557E] Ann and Half-Palindrome (Trie树+子串排序)

Codeforces - 557E

给定一个只有a和b的字符串,输出它第 K 个半回文子串
半回文串的定义是,所有奇数位置都是回文的
其中字符串长度不超过5000 ,保证有解


可以直接暴力找出所有半回文串,复杂度 (NlogN)

问题就在于如何找第 K大的,直接暴力排序是不支持的
我太久没做字符串题了,套路全忘光了
于是偷瞄了一下题解,看到了一个Trie这个单词
然后一下子就脑补出怎么做了 orzzzzzzzzzz

大概就是把所有的子串全部插入一个trie中
记录下每个子串的末尾在 trie中的位置,
这样一来每次只插入一个字符,复杂度就是 (N2)
然后再暴力找半回文串,找到之后在trie上加一下权值
最后中序dfs这棵trie树,就能找到第 K大啦

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))
#define PCUT puts("\n----------")

const int maxl=5e3+3;
struct Trie
{
    struct node
    {
        char chr;
        int val, nxt[2];
    };
    vector<node> T;
    int len,tot;
    char ans[maxl];
    void init()
    {
        T.clear();
        T.push_back({0,0,0,0});
        tot=0;
    }
    int append(int,char);
    void update(int);
    int find(int,int=0,int=0);
};

char in[maxl];
int N, K, tail[maxl][maxl];
bool hp[maxl][maxl];

Trie tree;

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    while(~scanf(" %s%d", in, &K))
    {
        CLR(tail); CLR(hp);
        tree.init();
        N = strlen(in);
        for(int i=0; i<N; i++)
        {
            tail[i][i] = tree.append(0, in[i]);
            for(int j=i+1; j<N; j++) tail[i][j] = tree.append(tail[i][j-1], in[j]);
        }
        for(int l=1; l<=N; l++) for(int i=0,j=i+l-1; j<N; i++,j++) if(in[i]==in[j])
        {
            int ii=i+2, jj=j-2;
            if(ii<=jj && !hp[ii][jj]) continue;
            hp[i][j] = 1;
            tree.update(tail[i][j]);
        }
        int len = tree.find(K);
        reverse(tree.ans, tree.ans+len);
        puts(tree.ans);
    }
    return 0;
}

int Trie::append(int pos, char chr)
{
    chr -= 'a';
    if(!T[pos].nxt[chr])
    {
        T.push_back({char(chr+'a'),0,0,0});
        T[pos].nxt[chr] = T.size()-1;
    }
    return T[pos].nxt[chr];
}

void Trie::update(int pos)
{
    T[pos].val ++;
}

int Trie::find(int K, int pos, int d)
{
    int nxp;
    tot += T[pos].val;
    if(tot>=K)
    {
        ans[0] = T[pos].chr;
        return len = d;
    }
    if(nxp = T[pos].nxt[0])
    {
        if(find(K, nxp, d+1))
        {
            ans[len-d] = T[pos].chr; 
            return len;
        }
    }
    if(nxp = T[pos].nxt[1])
    {
        if(find(K, nxp, d+1))
        {
            ans[len-d] = T[pos].chr;
            return len;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值