CF557E Ann and Half-Palindrome 字典树+dp
现在看这道题也不难啊,不知道考场上为啥没切~
code:
#include <bits/stdc++.h>
#define N 5006
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
struct trie
{
int size,tag,ch[2];
}p[N*2500];
char S[N];
int f[N][N],tot;
void dfs(int u)
{
p[u].size=p[u].tag;
for(int i=0;i<2;++i)
{
if(p[u].ch[i])
{
int v=p[u].ch[i];
dfs(v);
p[u].size+=p[v].size;
}
}
}
void dfs2(int u,int kth)
{
kth-=p[u].tag;
if(kth<=0)
{
exit(0);
}
for(int i=0;i<2;++i)
{
if(p[u].ch[i])
{
int v=p[u].ch[i];
if(kth>p[v].size) kth-=p[v].size;
else {
printf("%c",i+'a');
dfs2(v, kth);
break;
}
}
}
}
int main()
{
int i,j,n,k,rt=0;
// setIO("input");
scanf("%s%d",S+1,&k);
n=strlen(S+1);
for(i=1;i<=n;++i) f[i][i]=1;
for(i=n-1;i>=1;--i)
{
for(j=i+1;j<=n;++j)
{
if(j-2>=i+2)
{
if(S[i]==S[j] && f[i+2][j-2]) f[i][j]=1;
}
else
{
if(S[i]==S[j]) f[i][j]=1;
}
}
}
for(i=1;i<=n;++i)
{
rt=0;
for(j=i;j<=n;++j)
{
int c=S[j]-'a';
if(!p[rt].ch[c])
{
p[rt].ch[c]=++tot;
}
rt=p[rt].ch[c];
p[rt].tag+=f[i][j];
}
}
dfs(0);
dfs2(0,k);
return 0;
}
CF557E Ann and Half-Palindrome 字典树+dp的更多相关文章
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...
- Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp
C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- HDU5715 XOR 游戏 二分+字典树+dp
当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...
- BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)
<题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- LA 3942 - Remember the Word 字典树+DP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
随机推荐
- 关联安装 mysql ,zabbix , nginx, php
/usr/local/zabbix-3.2.6 /usr/local/php-5.6.3 /usr/local/mysql-5.7.26 安装mysql mv /etc/yum.repos.d/Cen ...
- PAT(B)1015 德才论(C)
题目链接:1015 德才论 (25 point(s)) 分析 由题意可知,需要将考生按照分数进行一个分类(级),然后在每一级中按照分数排序.输入的时候将每个人的总分,等级和录取人数先算出来.然后按照自 ...
- jq勾选
1.取消勾选 $("box").attr("checked", false); 2.勾选 $("kbox").attr("chec ...
- 解决使用绝对定位absolute后,margin:0 auto居中方法失效(转)
https://blog.csdn.net/qq_40678503/article/details/82780680
- The Heaviest Non-decreasing Subsequence Problem
最长非递减子序列变形题,把大于等于10000的copy五次放回去就可以了 ac代码: #include <cstdio> #include <cstring> #include ...
- (二十二)SpringBoot之使用mybatis generator自动生成bean、mapper、mapper xml
一.下载mybatis generator插件 二.生成generatorConfig.xml new一个generatorConfig.xml 三.修改generatorConfig.xml 里面的 ...
- 使用Lua编写Wireshark插件解析KCP UDP包,解析视频RTP包
前段时间写了一个局域网音视频通话的程序,使用开源 KCP 来实现可靠UDP传输. 通过研究发现KCP在发包时,会在数据包前面加上它自己的头.如果数据包较小,KCP可能会把多个数据包合成一个包发送,提高 ...
- swith-case 日历
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- stm32 SysTick系统定时器
它是一个24位向下递减的定时器,每计数一次所需时间为1/SYSTICK,SYSTICK是系统定时器时钟,它可以直接取自系统时钟,还可以通过系统时钟8分频后获取 当定时器计数到0时,将从LOAD 寄存器 ...
- css垂直居中如何实现
利用CSS3的transform:translate .center{ width:%; position: absolute; top: %; left: %; -moz-transform: tr ...