@Synchronized Subsequence@
@题目描述 - English@
Time limit : 2sec / Memory limit : 1024MB
Score : 1600 points
Problem Statement
You are given a string S of length 2N, containing N occurrences of a and N occurrences of b.
You will choose some of the characters in S. Here, for each i=1,2,…,N, it is not allowed to choose exactly one of the following two: the i-th occurrence of a and the i-th occurrence of b. (That is, you can only choose both or neither.) Then, you will concatenate the chosen characters (without changing the order).
Find the lexicographically largest string that can be obtained in this way.
Constraints
1≤N≤3000
S is a string of length 2N containing N occurrences of a and N occurrences of b.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the lexicographically largest string that satisfies the condition.
Sample Input 1
3
aababb
Sample Output 1
abab
【A subsequence of T obtained from taking the first, third, fourth and sixth characters in S, satisfies the condition.】
Sample Input 2
3
bbabaa
Sample Output 2
bbabaa
【You can choose all the characters.】
Sample Input 3
6
bbbaabbabaaa
Sample Output 3
bbbabaaa
Sample Input 4
9
abbbaababaababbaba
Sample Output 4
bbaababababa
@题目大意@
一个长度为 2∗N 2 ∗ N 的字符串S包含着 N N 个a与
个b。选择其中的一些字母,要求第i个a与第i个b要么都选要么都不选。不改变顺序地连接这些字母组成一个新串。
给定N与S,找到按照这个方法得到的最大字典序串。
@分析@
第一对(a,b)具有特殊性,因为第一对(a,b)之间全是相同的字母,所以我们先考虑它。
假如不保留第一对,则我们就要在第2~N对之间寻找答案。
假如保留第一对,我们分为两类讨论:
1)第一个a在第一个b前,即 aaaa…ab+s′ a a a a … a b + s ′ 。贪心地考虑,不难发现第一对(a,b)之间的a不保留要比保留要优秀。假设第一个b前面的a是第k个,则总的最优串 = 第一对(a,b) + 第k+1~N个对(a,b)所得到的最优串
2)第一个b在第一个a前,即 bbbb…ba+s′ b b b b … b a + s ′ 。依然贪心地考虑,我们发现第一对(a,b)之间的b保留要比不保留要优秀。之后的结论不同于上一类,因为我们保留了会对后面结果产生的(a,b),结论不再是“总的最优串 = 第1~k对(a,b) + 第k+1~N个对(a,b)所得到的最优串”【考场上就是卡在这个点上了】具体是什么我们待会儿细致地说。
初步分析可以大致得到这样一个结论:该问题具有最优子结构的性质!!!
这极有可能是一道dp题!!!
@详细的解析@
定义状态 dp[i][j] d p [ i ] [ j ] 为一个0-1状态,表示使用第i~N对串得到的最优串中,1表示第j对保留,0表示第j对不保留。
如果不选第i对,则: