Codeforces 557 E Ann and Half-palindrome

Source: Internet
Author: User

Test instructions is required to make a string of the first K-large half palindrome string


The definition of a half palindrome is: If a string is actually positioned at 1, then when all the odd digits I, and i<= (S.LENGTH+1/2), satisfies s[i]=s[s.length-i+1],

Then this string is half a palindrome.


The way to do this is to make a dictionary tree of all the half-palindrome strings of this string, and then check the size of K.


#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;struct node{char val;int vis,sum;node *next[2];}; String s,ans;void DFS (node *now,int k) {if (now->val< ' C ') {ans+=now->val;k-=now->vis;if (k<1) return;} if (now->next[0]==null) {if (now->next[1]==null) Return;dfs (now->next[1],k);} Else{if (now->next[1]==null) DFS (now->next[0],k); else{if (now->next[0]->sum>=k) DFS (now->next[0] , k); Elsedfs (now->next[1],k-now->next[0]->sum);}}} BOOL Dp[5010][5010];void DP (int n) {for (int i=0;i<n;i++) dp[i][i]=1;for (int i=1;i<n;i++) if (S[i-1]==s[i]) dp[i-1] [I]=1;for (int i=2;i<n;i++) if (S[i-2]==s[i]) dp[i-2][i]=1;for (int i=3;i<n;i++) if (S[i-3]==s[i]) dp[i-3][I]=1;for (int i=5;i<=n;i++) for (int j=0;j+i-1<n;j++) if (s[j]==s[j+i-1]&&dp[j+2][j+i-3]) dp[j][j+i-1]= 1;} int bg,ed;void Add (node *now,int index) {for (int i=0;i<2;i++) if (s[index]== ' a ' +i) {if (now->next[i]==null) {now- >next[i]=new Node;memset (now->next[i],0,sizeof (node)); Now->next[i]->val=s[index];} if (index==ed) {now->next[i]->vis++;now->next[i]->sum++;} Else{if (Dp[bg][index]) {now->next[i]->vis++;now->next[i]->sum++;} Add (now->next[i],index+1);}} now->sum=now->vis;for (int i=0;i<2;i++) if (now->next[i]!=null) now->sum+=now->next[i]->sum;} int main () {cin>>s;int k;cin>>k;int n=s.length ();Dp(n); node *fs=new node;memset (fs,0,sizeof (node)); fs- >val= ' C '; for (int i=0;i<n;i++) for (int j=n-1;j>=i;j--) if (Dp[i][j]) {bg=i;ed=j;add (fs,i); DFS (fs,k); Cout<<ans;}


Time limit per test1.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Tomorrow Ann takes the hardest exam of programming where she should get an excellent mark.

On the last theoretical class the teacher introduced the notion of a half-palindrome.

StringTis aHalf-palindrome, if for all the odd positionsI() The following condition is held: Ti? =? T | T|? -? i? +?1 , where| T|is the length of stringTIf positions is indexed from1. For example, strings "Abaa", "a", "BB", "Abbbaa"Is half-palindromes and strings"AB", "BBA"and"Aaabaa"is not."

Ann knows that on the exam she'll get strings, consisting only of lettersaandb, and numberk. To get an excellent mark she had to find thek-th in the lexicographical order string among all substrings ofsThat is half-palyndromes. Note that all substring in this order is considered as many times as many times it occurs ins.

The teachers guarantees that the given number K doesn ' t exceed the number of substrings of the given a string that Is Half-palindromes.

Can cope with this problem?

Input

The first line of the input contains strings(1?≤?| S|? ≤?5000), consisting only of characters 'a' and 'b', where| S|is the length of strings.

The second line contains a positive an integer K -the lexicographical number of the requested string among all the Half-palindrome substrings of the given string s. The strings is numbered starting from one.

It is guaranteed that number K doesn ' t exceed the number of substrings of the given string that was Half-palindro Mes.

Output

Print a substring of the given string that's the K-th in the lexicographical order of all substrings of the Give n String that is half-palindromes.

Sample Test (s) input
Abbabaab7
Output
Abaa
Input
Aaaaa10
Output
Aaa
Input
Bbaabb13
Output
Bbaabb
Note

By definition, string a? =? a 1 a 2... a N is lexicographically less than string b? =? b 1 b 2... b m , if eitherais a prefix ofband doesn ' t coincide withb, or there exists suchI, thata1?=?b1,?a2?=?b2,?...a i?-? 1?=?b i?-? 1,?aI? <?bI.

In the first sample Half-palindrome substrings is the following strings-a,a,a,a,AA,ABA,Abaa,Abba,Abbabaa,b,b,b,b,Baab,Bab,BB,Bbab,Bbabaab(the list is given in the lexicographical order).



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

Codeforces 557 E Ann and Half-palindrome

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.