[字典树] Codeforces 557E Ann and Half-Palindrome
来源:互联网 发布:巴西经济现状数据 编辑:程序博客网 时间:2024/05/01 09:05
题意:
给一个长度为5000的ab串,问你第k大的半回文子串是什么
所谓的半回文串就是下标是奇数的位置前后相等就好了。
思路:
首先发现串的长度只有5000,可以做一个类似区间dp的预处理
处理出dp[i][j]代表第i到j子串是不是半回文子串
然后依次把原串的所有子串插入字典树,并在节点标记个数
然后最后dfs一下k个就好了
代码:
#include"cstdlib"#include"cstdio"#include"cstring"#include"cmath"#include"queue"#include"algorithm"#include"iostream"using namespace std;#define N 20000007char v[5555],ans[5555];int dp[5555][5555],k;struct Trie{ int next[N][2],mark[N]; int root,L; int newnode() { memset(next[L],-1,sizeof(next[L])); mark[L++]=0; return L-1; } void go() { L=0; root=newnode(); } void init(int l,int r) { int p=root; for(int i=l; i<=r; i++) { int tep=v[i]-'a'; if(next[p][tep]==-1) next[p][tep]=newnode(); p=next[p][tep]; if(dp[l][i]) mark[p]++; } } void dfs(int x,int y) { if(k-mark[x]>0) { k-=mark[x]; for(int i=0; i<2; i++) { ans[y]='a'+i; if(next[x][i]!=-1) dfs(next[x][i],y+1); if(k<=0) return ; } } else { k-=mark[x]; ans[y]='\0'; return ; } }} ac;int main(){ while(scanf("%s",v)!=-1) { scanf("%d",&k); int len=strlen(v); for(int i=0;i<len;i++) for(int j=0;j<len;j++) dp[i][j]=0; for(int i=0; i<len; i++) { dp[i][i]=1; if(v[i]==v[i+1]) dp[i][i+1]=1; if(v[i]==v[i+2]) dp[i][i+2]=1; if(v[i]==v[i+3]) dp[i][i+3]=1; } for(int i=4; i<len; i++) for(int j=0; j+i<len; j++) if(v[j]==v[j+i]&&dp[j+2][j+i-2]) dp[j][j+i]=1; ac.go(); for(int i=0; i<len; i++) ac.init(i,len-1); ac.dfs(0,0); puts(ans); } return 0;}
0 0
- [字典树] Codeforces 557E Ann and Half-Palindrome
- Codeforces 557E Ann and Half-Palindrome DP+字典树
- [字典树] Codeforces 557E Ann and Half-Palindrome
- Codeforces 577E Ann and Half-Palindrome 字典树
- codeforces 557 E Ann and Half-Palindrome
- Codeforces 557E - Ann and Half-Palindrome (字典树+DP)
- Codeforces 557E Ann and Half-Palindrome (字典树+字符串排序)
- Codeforces 557E Ann and Half-Palindrome (Trie树)
- cf #311 E. 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 Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树
- [Codeforces 557E] Ann and Half-Palindrome (Trie树+子串排序)
- Codeforces 311(div 2):E. Ann and Half-Palindrome
- codeforces #311 557E E. Ann and Half-Palindrome(trie+dp+dfs)
- 找出s的子串中字典序第k小的“半回文串” Trie Codeforce Div. 2 Ann and Half-Palindrome
- [codeforces] 501E - Misha and Palindrome Degree
- codeforces 665E (字典树)
- Android中批处理drawable-xxx目录中图片资源的那些事儿
- fo1 && fo2 | fo3 > fo4 ;正则
- 《暗时间》读书笔记
- 网络协议分析工具Wireshark
- IPM
- [字典树] Codeforces 557E Ann and Half-Palindrome
- 2015/08/04
- srs之hls
- 【黑马程序员】Java笔记--单例设计模式
- 深入Java 类的初始化
- 代码整洁之道--类
- Linux性能指标分析
- 86. Partition List
- 解决虚拟机打不开Ubuntu的问题