cf #311 E. Ann and Half-Palindrome (dp+字典树)
来源:互联网 发布:centos ibus 设置 编辑:程序博客网 时间:2024/05/01 18:07
题目:http://codeforces.com/contest/557/problem/E
题意:定义半回文串:字符串前面一半的奇数位上的字符和后一边对应位置上的字符相同的字符串。给定长度|s|<=5000的字符串,求它的半回文子串中,字典序第K小的半回文子串。
分析:定义dp[i][j]表示区间[i,j]代表的子串是否为半回文串。然后n^2推出所有的半回文子串。将所有的半回文子串插入字典树,然后遍历一下字典树,找到正确的位置输出就行了。
代码:
#include <bits/stdc++.h>using namespace std;typedef long long LL;typedef unsigned long long ULL;const LL INF = 1E9+9;const int maxn = 5555;char s[maxn];int n,k;bool dp[maxn][maxn],hasFind;int trie[maxn*maxn][2],cnt,root,cover[maxn*maxn],fa[maxn*maxn];int newnode(){trie[cnt][0]=trie[cnt][1]=-1;cover[cnt]=0;return cnt++;}void Init(){cnt=0;root=newnode();}void Insert(int start){int cur=root;for(int i=start;i<n;i++){int index=s[i]-'a';if(trie[cur][index]==-1)trie[cur][index]=newnode();fa[trie[cur][index]]=cur;cur=trie[cur][index];if(dp[start][i])cover[cur]++;}}void dfs(int cur){if(hasFind)return ; k-=cover[cur];if(k<=0){hasFind=true;int f=0;while(cur){s[f++]=(trie[fa[cur]][0]==cur?'a':'b');cur=fa[cur];}for(int i=f-1;i>=0;i--)putchar(s[i]);return ;}for(int i=0;i<2;i++)if(trie[cur][i]!=-1)dfs(trie[cur][i]);}int main(){scanf("%s%d",s,&k);n=strlen(s); for(int i=n-1;i>=0;i--)for(int j=i;j<n;j++)dp[i][j]=(j-i<=4?(s[i]==s[j]):((s[i]==s[j])&&dp[i+2][j-2]));Init();for(int i=0;i<n;i++)Insert(i);dfs(root);return 0;}
0 0
- cf #311 E. Ann and Half-Palindrome (dp+字典树)
- Codeforces 557E Ann and Half-Palindrome DP+字典树
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome (DP+字典树)
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
- Codeforces 577E Ann and Half-Palindrome 字典树
- [字典树] Codeforces 557E Ann and Half-Palindrome
- [字典树] Codeforces 557E Ann and Half-Palindrome
- Codeforces 557E - Ann and Half-Palindrome (字典树+DP)
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树
- codeforces #311 557E E. Ann and Half-Palindrome(trie+dp+dfs)
- Codeforces 557E Ann and Half-Palindrome (字典树+字符串排序)
- Codeforces 311(div 2):E. Ann and Half-Palindrome
- Codeforces 557E Ann and Half-Palindrome (Trie树)
- codeforces 557 E Ann and Half-Palindrome
- [Codeforces 557E] Ann and Half-Palindrome (Trie树+子串排序)
- 找出s的子串中字典序第k小的“半回文串” Trie Codeforce Div. 2 Ann and Half-Palindrome
- CF - 665E 字典树
- 【cf 570e】Pig and Palindromes dp
- 关于提高MYSQL性能的几个经验
- C++与汇编
- composer安装yii2
- LeetCode-Add Two Numbers
- HDU 1754 I Hate It(线段树)
- cf #311 E. Ann and Half-Palindrome (dp+字典树)
- Apache Shiro 快速入门教程,shiro 基础教程
- android学习之路(七)---- 用Fan-Image-Loader实现一个炫酷的相册功能
- C++的namespace
- IOS--最新版SDWebImage的使用
- app微信登录的server
- core标签foreach的使用详解
- 初识C#.net
- php中$_SERVER的意义