题意简述

给定一个字符串(长度不超过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. 所有硬币组合问题——动态规划hdu2069

    Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cen ...

  2. L2 Regularization for Neural Nerworks

    L2 Regularization是解决Variance(Overfitting)问题的方案之一,在Neural Network领域里通常还有Drop Out, L1 Regularization等. ...

  3. Autoencoder基本操作及其Tensorflow实现

    最近几个月一直在和几个小伙伴做Deep Learning相关的事情.除了像tensorflow,gpu这些框架或工具之外,最大的收获是思路上的,Neural Network相当富余变化,发挥所想.根据 ...

  4. 跨域资源共享(CORS)-漏洞整理

    绕过方法整理 绕过 - 仅对域名校验 #POC #"Access-Control-Allow-Origin: https://xx.co & Access-Control-Allow ...

  5. Eclipse+Pydev环境下出现error “eclipse Non-UTF-8 code”

    文件首行加上”#coding=utf-8”   ,这一句话可控制代码中可输入中文字符

  6. 批量更新:A表数据源 B表目标

    update a set a.Title = b.Title from Table_A a ,Tbale_B b where a.ID_Table_B = b.ID

  7. IDF-CTF-简单的js加密 writeup

    题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=43 知识点:js语法 这里这里→ http://c ...

  8. Hibernate的dtd文件和properties文件

    hibernate-configuration-3.0.dtd <!-- Hibernate file-based configuration document. <!DOCTYPE hi ...

  9. Rest接口单元测试

    Get请求url不超过4000字节 Rest成熟度:level 0:使用http作为传输方式,leve 1:引入资源概念,每个资源有对应的url,level 2:使用http方法进行不同操作,使用ht ...

  10. Django读写分离

    多数据库配置 数据库配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join ...