codeforces 557E Ann and Half-Palindrome
题意简述
给定一个字符串(长度不超过5000 且只包含a、b)求满足如下所示的半回文子串中字典序第k大的子串
ti = t|t| - i + 1(|t|为字符串长度) 
----------------------------------------------------------------------------------
比赛时看到回文串 再看到字典序第k大 满满的后缀数组一类的算法的即视感
考虑到编程复杂度以及自己本来就不熟练 于是直接放弃了
结果比赛完后听说不少人都是直接上trie树后暴力求解 才发现字符串长度不超过5000
然而没注意到只包含a、b 当成了包含所有小写字母以至于脑洞大开
去想什么通过中序遍历来压缩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的更多相关文章
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...
- Codeforces Beta Round #7 D. Palindrome Degree hash
D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...
- Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希
题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...
- 【codeforces 798A】Mike and palindrome
[题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...
- Codeforces 877F Ann and Books 莫队
转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...
- Codeforces 577E Ann and Half-Palindrome 字典树
题目链接 题意: 若一个字符串是半回文串.则满足第一位和最后一位相等, 第三位和倒数第三位相等.如此类推. 给定一个字符串s,输出s的全部子串中的半回文串字典序第k大的 字符串. good[i][j] ...
- Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)
题意:给你一个长度为偶数n的数组,每次可以将一个元素修改为不大于k的值,要求每个a[i]+a[n-i+1]都相等,求最少操作多少次 题解:假设每一对的和都为sum,小的记为mn,大的记为mx; ...
随机推荐
- Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...
- Sublime Text3安装配置
Sublime Text3的所有package都可在如下网站检索 https://packagecontrol.io/browse 以下是我的插件列表 CoolFormat 一款C\C++\C#\CS ...
- 07 (H5*) js课程第8天 高阶函数、闭包、沙箱
目录: 1:call和apply方法调用 2:bind方法复制 3:函数中的几个属性值 4:高阶函数,函数作为参数 5:高阶函数,函数作为返回值. 6: 作用域链,作用域,预解析 7:闭包--延长 ...
- sqlserver关于时间的一些语句
/* 去掉时间的时分秒 */ CONVERT(CHAR(10),operate_time,120) /* 所有的天数增加一天 */ DATEADD(day,1,t.operate_time) /* 返 ...
- Redis进行数据同步
数据库中的数据一般都涉及到需要对数据进行备份的,这样可以保证数据的安全性,并且如果将一个主设备的数据同步到多个从设备上,允许用户访问数据时可以从多个从设备进行读取, 这样还可以缓解主设备的压力,Red ...
- 常用jQuery技巧总结
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- dp或dfs(01背包问题)
链接:https://ac.nowcoder.com/acm/contest/993/C来源:牛客网题意:n头牛,给出它们的H高度,问这些牛的高度叠加起来大于等于书架高度,问叠加后的高度与书架的差值最 ...
- 远程连接SuSE系统的配置方法
今天,在VMware上搭建了SuSE Linux系统,使用xshell远程进行连接,一直连接不上,后来百度了一下,连接成功,这里总结一下配置的办法: (1):关闭防火墙 (2):配置sshd( Pas ...
- hdu5858 Hard problem(求两圆相交面积)
题目传送门 Hard problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 浅谈XML涉及到的常见技术(编写+解析)
xml:即可扩展标记语言,用于描述关系型数据,也经常用作软件的配置文件: 1,编写xml文档一般基于一个约束文档,该文档用于规定xml的书写规范,常用的约束技术有 (1)XML ...