Codeforces Round #311 (Div. 2) E. Ann and Half-palindrome (dp+ dictionary tree)

Source: Internet
Author: User

Title Address: Portal
First use DP to find all the matching requirements of the semi-palindrome, marked out. Then construct the dictionary tree. Then DFS again to find out all the nodes of the subtrees, the last search will be able to identify the first K.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)//#pragma comment (linker, "/stack:1024000000")Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=400000+Ten;intok[5001][5001];intK, CNT;Chars[6000], str[6000];structnode{intFlag, sum; Node *next[2];}; Node *newnode () {intI Node *p=NewNode for(i=0;i<2; i++) {p->next[i]=null; } p->flag=0; p->sum=0;returnP;}voidInsert (intStintLen, node *root) {intI, X; Node *p=root; for(i=st;i<len;i++) {x=s[i]-' A ';if(p->next[x]==null) P->next[x]=newnode (); p=p->next[x];if(Ok[st][i])                        {p->flag++;                p->sum++; }        }}voidDFS (node *u) { for(intI=0;i<2; i++) {if(U->next[i]!=null)                        {DFS (u->next[i]);                u->sum+=u->next[i]->sum; }        }}voidSeach (node *root) {node *p=root; while(1) {k-=p->flag;if(k<=0){return; }if(p->next[0]!=null) {if(p->next[0]->sum>=k) {str[cnt]=' A '; p=p->next[0]; cnt++;Continue; }Elsek-=p->next[0]->sum; } str[cnt]=' B '; p=p->next[1];        cnt++; }}intMain () {intI, J, Len, H; while(scanf('%s ', s)!=eof) {scanf("%d", &k); len=strlen(s);memset(OK,0,sizeof(OK)); for(i=0; i<len;i++) {ok[i][i]=1;if(i<len-1&&s[i]==s[i+1]) ok[i][i+1]=1;if(i<len-2&&s[i]==s[i+2]) ok[i][i+2]=1;if(i<len-3&&s[i]==s[i+3]) ok[i][i+3]=1; } for(i=5; i<=len;i++) { for(j=0; j<len-i+1; j + +) {h=j+i-1;if(s[j]==s[h]&&ok[j+2][h-2]) ok[j][h]=1;                }} node *root; Root=newnode (); for(i=0; i<len;i++) {Insert (I, Len, root);                } dfs (root); Cnt=0; Seach (root); for(i=0; i<cnt;i++) {printf("%c", Str[i]); }puts(""); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #311 (Div. 2) E. Ann and Half-palindrome (dp+ dictionary tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.