算法:dp+字典树

题目链接Ann and Half-Palindrome

  在CF刷字符串题的时候遇到了这题,其实并没有黑题这么难,个人感觉最多是紫题吧(虽然一开始以为是后缀自动机的神仙题)。

  首先注意到字符串 \(s\) 长度很小( \(1\le|s|\le5000\) ),可以 \(\mathcal O(n^2)\) 地把所有子串求出来,再用Trie树存起来,这样就方便我们dfs求字典序第 \(k\) 小的半回文串。所以问题重心变为怎么快速判断这些子串是否为半回文串。根据半回文串的特点,长度长的半回文串是包含长度小的半回文串的,所以我们可以用区间dp解决。设 \(f[i][l]\) 表示 \(s[i,i+l-1]\) 是否为半回文串,它的转移方程可以写作( \([A]\) 表示 \(A\) 为真时值为1,否则为0):

\[\left\{\begin{array}{l}f[i][1]=1\\ f[i][2]=[s[i]=s[i+1]]\\f[i][3]=[s[i]=s[i+2]]\\f[i][4]=[s[i]=s[i+3]] \\ f[i][l]~=[~s[i]=s[i+l-1]\&\&f[i+2][l-4]~],l\ge5\end{array}\right.
\]

  这样dp的时间复杂度也是 \(\mathcal O(n^2)\) 的。之后dfs求解第 \(k\) 小的半回文串就比较简单了,由于Trie节点数也是 \(\mathcal O(n^2)\) 的,所以总时间复杂度是 \(\mathcal O(n^2)\) 的。

\(Code:\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e3 + 5;
bool f[N][N];
int trie[N * N][2],cnt,num[N * N],tot,k;
char s[N+6],ans[N]; void dfs(int now){
for(int i = 0;i <= 1;i++){
int next = trie[now][i];
if(next == 0) continue;
//如果该节点所表示的半回文串数量大于k,说明答案就是该半回文串。
if(k <= num[next]) {
ans[++tot] = i + 'a';
for(int j = 1; j <= tot; j++) printf("%c", ans[j]);
exit(0);
}
k -= num[next]; //k为全局变量
ans[++tot] = i + 'a';
dfs(next);
tot--;
}
} int main() {
scanf("%s %d",s+1,&k);
int len = strlen(s+1);
for(int i = 1;i <=len;i++) f[i][1] = 1;
for(int i = 1;i < len;i++) f[i][2] = s[i] == s[i+1];
for(int i = 1;i < len;i++) f[i][3] = s[i] == s[i+2];
for(int i = 1;i < len;i++) f[i][4] = s[i] == s[i+3];
for(int l = 5;l <= len;l++)
for(int i = 1; i <= len; i++)
f[i][l] = (s[i] == s[i+l-1] && f[i+2][l-4]);
//将s所有子串加入trie树中
for(int i = 1;i <= len;i++){
int now = 0;
for(int l = 1; l+i-1 <= len; l++){
int c = s[i+l-1] - 'a';
if(trie[now][c] == 0) trie[now][c] = ++cnt;
now = trie[now][c];
if(f[i][l]) num[now]++;
}
}
dfs(0);
return 0;
}

CF557E Ann and Half-Palindrome 题解的更多相关文章

  1. POJ3974:Palindrome——题解

    http://poj.org/problem?id=3974 题目大意: 求最大回文子串长度. ———————————————————— 马拉车板子题. 马拉车大概讲解: 首先在每两个字母之间插入‘# ...

  2. URAL1297:Palindrome——题解

    http://acm.timus.ru/problem.aspx?space=1&num=1297 https://vjudge.net/problem/URAL-1297 给定一个字符串,求 ...

  3. CF557E Ann and Half-Palindrome 字典树+dp

    现在看这道题也不难啊,不知道考场上为啥没切~ code: #include <bits/stdc++.h> #define N 5006 #define setIO(s) freopen( ...

  4. Valid Palindrome leetcode java

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  5. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  7. 2013-2014集训之DP

    第一周: 经过漫长的时间,终于有时间来写一下结题报告. 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=36180#overview A ...

  8. A. Karen and Morning

    A. Karen and Morning time limit per test 2 seconds  memory limit per test 512 megabytes input standa ...

  9. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

随机推荐

  1. MySQL 多实例及其主从复制

    目录 Mysql 实例 Mysql 多实例 创建多实例目录 编辑配置文件 初始化多实例数据目录 授权目录 启动多实例 连接多实例并验证 Mysql 多实例设置密码 设置密码后连接 Mysql 多实例主 ...

  2. spring再学习之AOP实操

    一.spring导包 2.目标对象 public class UserServiceImpl implements UserService { @Override public void save() ...

  3. Service Cloud 零基础(四)快速配置一个问卷调查(无开发)

    本篇参考:https://trailhead.salesforce.com/content/learn/modules/survey-basics 我们在工作和生活中会经历过形形色色得调查问卷,有一些 ...

  4. C# 类 (6) -继承

    继承 定义类的时候,public class Dog:Animal 表示 Dog 这个类是 继承自 Animal,冒号后面的是它的基类 继承后 的Dog 类,当调用Dog.Great() 的时候输出的 ...

  5. 微软官方 free 教程 & 教材 ,MVC ,ASP.NET,.NET,

    MVA https://mva.microsoft.com/ebooks free ebooks 微软官方, free, 教程 ,教材,微软官方 free 教程 & 教材,MVC ,ASP.N ...

  6. CSS 定位 relative && absolute 问题?

    1 1 1 CSS 定位 relative && absolute 问题? 谁能解释一下,为什么div使用 relative是设置right,bottom 后,看不到div 呀,哪里多 ...

  7. styled-components all in one

    styled-components all in one CSS in JS https://www.styled-components.com/ https://github.com/styled- ...

  8. Visual Studio Online & Web 版 VS Code

    Visual Studio Online & Web 版 VS Code https://online.visualstudio.com https://devblogs.microsoft. ...

  9. AMP ⚡️原理

    AMP ️原理 AMP 是如何运作的 https://amp.dev/zh_cn/about/how-amp-works/ AMP 瞬时加载 结合了以下优化是 AMP 页面速度之快以至于它们可以瞬时加 ...

  10. QR code & auto login & OAuth 2.0

    QR code & auto login & OAuth 2.0 扫码自动登录原理解析 https://www.ximalaya.com/shangye/14685881/958686 ...