E. Ann and Half-Palindrome
time limit per test1.5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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.
String t is a half-palindrome, if for all the odd positions i () the following condition is held: ti = t|t| - i + 1, where |t| is the length of string t if positions are indexed from 1. For example, strings “abaa”, “a”, “bb”, “abbbaa” are half-palindromes and strings “ab”, “bba” and “aaabaa” are not.
Ann knows that on the exam she will get string s, consisting only of letters a and b, and number k. To get an excellent mark she has to find the k-th in the lexicographical order string among all substrings of s that are half-palyndromes. Note that each substring in this order is considered as many times as many times it occurs in s.
The teachers guarantees that the given number k doesn’t exceed the number of substrings of the given string that are half-palindromes.
Can you cope with this problem?
Input
The first line of the input contains string s (1 ≤ |s| ≤ 5000), consisting only of characters ‘a’ and ‘b’, where |s| is the length of string s.
The second line contains a positive integer k — the lexicographical number of the requested string among all the half-palindrome substrings of the given string s. The strings are numbered starting from one.
It is guaranteed that number k doesn’t exceed the number of substrings of the given string that are half-palindromes.
Output
Print a substring of the given string that is the k-th in the lexicographical order of all substrings of the given string that are half-palindromes.
Sample test(s)
input
abbabaab
7
output
abaa
input
aaaaa
10
output
aaa
input
bbaabb
13
output
bbaabb
Note
By definition, string a = a1a2… an is lexicographically less than string b = b1b2… bm, if either a is a prefix of b and doesn’t coincide with b, or there exists such i, that a1 = b1, a2 = b2, … ai - 1 = bi - 1, ai < bi.
In the first sample half-palindrome substrings are 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).
題意要輸出奇回文子串的第k個字典序的串,奇回文串,就是只要求奇數位置滿足回文串的要求就可以了!
這用dp推下就可以了,dp[i][j]表從i到j是否是回文串
則dp[i][j] = dp[i+2][j-2](j-i>=4),其他的都是奇回文串;
要輸出第k個字典序的子串,首先肯定想到要用字典樹輸出來就是了,很簡單的dfs就可以了。插入串的時候,就不要一個一個插了,而直接插一個後綴就可以了這樣總的復雜度也就是O(n * n),n是串的長度。
#define N 5050
#define M 100005
#define maxn 205
#define maxnode 20000050
#define sigma_size 2
#define MOD 1000000000000000007
int n,len,ansi,sum;
bool dp[N][N];
char str[N],ans[N];
struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void Init(){sz = 1;val[0]=0;memset(ch[0],0,sizeof(ch[0]));}
Trie(){}
int idx(char c){ return c - 'a';}
void insert(char * s,int l,int r){
int u = 0;
for(int i=l;i<=r;i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz],0,sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz ++;
}
u = ch[u][c];
if(dp[l][i])val[u]++;
}
}
bool find(int root,int k){
sum = sum + val[root];
if(sum >= k) {
ansi = 0;
return true;
}
FI(sigma_size){
if(ch[root][i]){
int u = ch[root][i];
if(find(u,k)){
ans[ansi++] = i;
return true;
}
}
}
return false;
}
};
Trie T;
int main()
{
while(SS(str)!=EOF)
{
T.Init();
S(n);
len = strlen(str);
FI(len) dp[i][i] = true;
for(int l = 1;l=0;i--){
printf(%c,ans[i] + 'a');
}
printf(
);
}
return 0;
}