题意简述

给定一个字符串(长度不超过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. Hadoop: 在Azure Cluster上使用MapReduce

    Azure对于学生账户有260刀的免费试用,火急火燎地创建Hadoop Cluster!本例子是使用Hadoop MapReduce来统计一本电子书中各个单词的出现个数. Let's get hand ...

  2. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  3. JPA Example查询

    //创建查询条件数据对象 Customer customer = new Customer(); customer.setAddress("河南省郑州市"); customer.s ...

  4. eclipse新建maven项目出错 pom.xml报错

    问题: 1.新建项目后会提示一个这样的错 maven-compiler-plugin:3.1:compile(1 errors) maven-compiler-plugin:3.1:testCompi ...

  5. 【JMeter4.0】一、JAVA环境-JDK1.10安装与配置

    环境变量的作用: 它是操作系统用来指定运行环境的一些参数.比如临时文件夹位置和系统文件夹位置等.当你运行某些程序时,除了在当前文件夹中寻找外,还会到这些环境变量中去查找,比如“Path”就是一个变量, ...

  6. C#面试 笔试题 四

    1.请你简单的说明数据库建立索引的优缺点 使用索引可以加快数据的查询速度,不过由于数据插入过程中会建索引,所以会降低数据的插入.更新速度,索引还会占磁盘空间. 2.什么是WEB服务控件?使用WEB服务 ...

  7. Quartz的简单使用

    一.Quartz 介绍 Quartz是Java领域最著名的.功能丰富的.开放源码的作业调度工具,几乎可以在所有的Java应用程序中集成--从小的单机应用到大的电子商务系统. Quartz可以用来执行成 ...

  8. 1. ZooKeeper简介

    1. ZooKeeper是什么 ZooKeeper致力于提供一个高性能.高可用,且具备严格的顺序访问控制能力的分布式协调服务,是雅虎公司创建,是Google的Chubby一个开源的实现,也是Hadoo ...

  9. 手模手配置Eslint,看懂脚手架中的Eslint

    使用ESLint前:eslint是干嘛的,我这样写有什么问题,怎么还报错了,太麻烦想去掉这个插件,脚手架中关于eslint文件里的配置是什么意思?怎么设置配置项和规则达到自己想要的检测效果呢?怎么集成 ...

  10. TCP为什么会采用三次握手,若采用二次握手可以吗?

    建立连接的过程是利用C/S(客户机/服务器)模式,假设A为客户端,B为服务器端. TCP是采用三次握手进行连接的,简要说明该过程: (1) A向B发送连接请求 (2) B对收的的A的报文段进行确认 ( ...