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; ...
随机推荐
- [COCI2017.1]Deda —— 解锁线段树的新玩法
众所周知,能用线段树做的题一定可以暴力 但考场上也只能想到暴力了,毕竟还是对线段树不熟练. deda 描述 有一辆车上有n个小孩,年龄为1~n,然后q个询问,M X A代表在第X站时年龄为A的小孩会下 ...
- JavaScript List
function List() { this.listSize = 0; this.pos = 0; this.dataSource = []; this.clear = fu ...
- 《JAVA设计模式》之解释器模式(Interpreter)
在阎宏博士的<JAVA与模式>一书中开头是这样描述解释器(Interpreter)模式的: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个 ...
- POJ1742 coins 动态规划之多重部分和问题
原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...
- 【题解】Hankson 的趣味题
题目大意 已知正整数$a_{0}$.$a_{1}$.$b_{0}$.$b_{1}$($1 \leq a_{0}, a_{1}, b_{0}, b_{1} \leq 2 \times 10^{9}$), ...
- Spacemacs 的配置
Spacemacs 的配置 */--> Spacemacs 的配置 Table of Contents 1. 安利 2. 安装 3. layer 4. 自带的 layer 5. better-e ...
- Codeforces - 1191C - Tokitsukaze and Discard Items - 模拟
https://codeforces.com/contest/1191/problem/C 一开始想象了一下,既然每次删除都是往前面靠,那么好像就是页数*页容量+空位数=最多容纳到的坐标. 至于为什么 ...
- Oracle 汉字占用字节数
在oracle中一个字符特别是中文字符占几个字节是与字符集有关的. 比如GBK,汉字就会占两个字节,英文1个:如果是UTF-8,汉字一般占3个字节,英文还是1个.但是一般情况下,我们都认为是 ...
- 65.Longest Increasing Subsequence(最长增长子序列)
Level: Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...
- CSS世界中的“盒子”
1.块级元素 HTML标签通常被分为两类:块级元素和内联元素. “块级元素”和“display为block的元素”不是同一个概念.例如<li>元素默认的display值为list-item ...