题意简述

给定一个字符串(长度不超过5000 且只包含a、b)求满足如下所示的半回文子串中字典序第k大的子串

ti = t|t| - i + 1(|t|为字符串长度)  

----------------------------------------------------------------------------------

比赛时看到回文串 再看到字典序第k大 满满的后缀数组一类的算法的即视感

考虑到编程复杂度以及自己本来就不熟练 于是直接放弃了

结果比赛完后听说不少人都是直接上trie树后暴力求解 才发现字符串长度不超过5000

然而没注意到只包含a、b 当成了包含所有小写字母以至于脑洞大开

去想什么通过中序遍历来压缩trie树空间之类的(这个只是理论上还不错 还没去实现过)

----------------------------------------------------------------------------------
正解的话 先可以通过DP求出符合要求的子串的左右端点
然后直接在trie树里统计即可
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#define rep(i,n) for(int i=1;i<=n;++i)
#define imax(x,y) (x>y?x:y)
#define imin(x,y) (x<y?x:y)
using namespace std;
const int N=;
struct trie
{
int cnt;
int nexte[];
}a[N*N>>];
bool f[N][N];
char s[N],ans[N];
int k,n,num=,lans=;
bool flag=;
void add_trie(int x,int l,int r)
{
int y=s[r]-'a';
if(!a[x].nexte[y])
a[x].nexte[y]=++num;
if(f[l][r])
++a[a[x].nexte[y]].cnt;
if(r<n)
add_trie(a[x].nexte[y],l,r+);
}
void inquiry_trie(int x)
{
for(int i=;i<;++i)
if(a[x].nexte[i])
{
if(a[a[x].nexte[i]].cnt)
{
k-=a[a[x].nexte[i]].cnt;
a[a[x].nexte[i]].cnt=;
if(k<=)
{
flag=;
ans[++lans]=i+'a';
return;
}
}
inquiry_trie(a[x].nexte[i]);
if(flag)
{
ans[++lans]=i+'a';
return;
}
}
}
int main()
{
scanf("%s%d",s+,&k);
n=strlen(s+);
rep(i,n)
{
f[i][i]=;
if(i+<=n&&s[i]==s[i+])f[i][i+]=;
if(i+<=n&&s[i]==s[i+])f[i][i+]=;
if(i+<=n&&s[i]==s[i+])f[i][i+]=;
}
for(int i=;i<=n;++i)
for(int j=;j+i-<=n;++j)
if(s[j]==s[j+i-]&&f[j+][j+i-])
f[j][j+i-]=;
rep(i,n)
add_trie(,i,i);
inquiry_trie();
for(int i=lans;i;--i)
printf("%c",ans[i]);
return ;
}

codeforces 557E Ann and Half-Palindrome的更多相关文章

  1. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  2. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  3. Codeforces Beta Round #7 D. Palindrome Degree hash

    D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...

  4. Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串

    题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...

  5. Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希

    题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...

  6. 【codeforces 798A】Mike and palindrome

    [题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...

  7. Codeforces 877F Ann and Books 莫队

    转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  8. Codeforces 577E Ann and Half-Palindrome 字典树

    题目链接 题意: 若一个字符串是半回文串.则满足第一位和最后一位相等, 第三位和倒数第三位相等.如此类推. 给定一个字符串s,输出s的全部子串中的半回文串字典序第k大的 字符串. good[i][j] ...

  9. Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)

    题意:给你一个长度为偶数n的数组,每次可以将一个元素修改为不大于k的值,要求每个a[i]+a[n-i+1]都相等,求最少操作多少次 题解:假设每一对的和都为sum,小的记为mn,大的记为mx;     ...

随机推荐

  1. urllib.parse解析链接

    1. urlparse() 解析链接,注意,返回值比3多一个params的属性 from urllib.parse import urlparse result = urlparse('http:// ...

  2. [Linux] 008 文件处理命令

    1. 文件处理命令:touch 命令名称:touch 命令所在路径:/bin/touch 执行权限:所有用户 语法:touch [文件名] 功能描述:创建空文件 范例: 文件名不包含空格 touch ...

  3. CentOS利用Lua访问Redis

    首先确保你编译的Lua是支持链接外部动态链接库的.因为在对Redis进行访问时是需要使用socket通信的, 而这依赖于外部的C语言写的动态连接库. 首先,这里先下载Redis的Lua客户端访问包re ...

  4. P4929 【模板】舞蹈链(DLX)

    题目背景 本题是舞蹈链模板——精确覆盖问题 题目描述 给定一个N行M列的矩阵,矩阵中每个元素要么是1,要么是0 你需要在矩阵中挑选出若干行,使得对于矩阵的每一列j,在你挑选的这些行中,有且仅有一行的第 ...

  5. 关于URL和URI的最简单理解

    以下面网址为例: http://www.sina.com/news/1.html 那么,http://www.sina.com/news/1.html就表示URL,用于标识互联网中的某一资源:/new ...

  6. 相对路径 分类: C# 2015-06-11 15:41 8人阅读 评论(0) 收藏

    .绝对路径     绝对路径是指文件在硬盘上真正存在的路径.例如"bg.jpg"这个图片是存放在硬盘的"E:\book\网页布局代码\第2章"目录下,那么 &q ...

  7. 攻防世界--python-trade

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/69c8f29912ae4f679d92a6cd36c33196.pyc 这里需要用到一 ...

  8. Java源码之ArrayList分析

    一.ArrayList简介 ArrayList底层的数据结构是数组,数组元素类型为Object类型,即可以存放所有类型数据. 与Java中的数组相比,它的容量能动态增长.当创建一个数组的时候,就必须确 ...

  9. 获取input输入值

  10. 部署Lighttpd到252板子

    1.先到lighttpd官网下载对应版本的软件包: 如: lighttpd-1.4.30.tar.gz 2. 将压缩包解压到任意目录得到文件夹 lighttpd-1.4.30 3. 执行配置命令:  ...