第六周训练题

B. Longest Palindrome
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings “pop”, “noon”, “x”, and “kkkkkk” are palindromes, while strings “moon”, “tv”, and “abab” are not. An empty string is also a palindrome.

Gildong loves this concept so much, so he wants to play with it. He has n distinct strings of equal length m. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.

Input
The first line contains two integers n and m (1≤n≤100, 1≤m≤50) — the number of strings and the length of each string.

Next n lines contain a string of length m each, consisting of lowercase Latin letters only. All strings are distinct.

Output
In the first line, print the length of the longest palindrome string you made.

In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don’t print this line at all.

Examples

inputCopy
3 3
tab
one
bat
outputCopy
6
tabbat
inputCopy
4 2
oo
ox
xo
xx
outputCopy
6
oxxxxo
inputCopy
3 5
hello
codef
orces
outputCopy
0

inputCopy
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
outputCopy
20
ababwxyzijjizyxwbaba
Note
In the first example, “battab” is also a valid answer.

In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.

In the third example, the empty string is the only valid palindrome string.

代码实践问题

题解思路很清晰,回文序列读完题目后,很清晰条件也很清晰感觉很好实践,但尝试去写时,会存在很多难以实践的问题,对字符串处理的能力欠缺,就像判断出来不相等做中间段时,还需要查找串中是否已经有中间段。
AC代码:https://blog.csdn.net/weixin_51443397/article/details/115642081?utm_source=app&app_version=4.5.8

Gildong recently learned how to find the longest increasing subsequence (LIS) in O(nlogn) time for a sequence of length n. He wants to test himself if he can implement it correctly, but he couldn’t find any online judges that would do it (even though there are actually many of them). So instead he’s going to make a quiz for you about making permutations of n distinct integers between 1 and n, inclusive, to test his code with your output.

The quiz is as follows.

Gildong provides a string of length n−1, consisting of characters ‘<’ and ‘>’ only. The i-th (1-indexed) character is the comparison result between the i-th element and the i+1-st element of the sequence. If the i-th character of the string is ‘<’, then the i-th element of the sequence is less than the i+1-st element. If the i-th character of the string is ‘>’, then the i-th element of the sequence is greater than the i+1-st element.

He wants you to find two possible sequences (not necessarily distinct) consisting of n distinct integers between 1 and n, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤104).

Each test case contains exactly one line, consisting of an integer and a string consisting of characters ‘<’ and ‘>’ only. The integer is n (2≤n≤2⋅105), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is n−1.

It is guaranteed that the sum of all n in all test cases doesn’t exceed 2⋅105.

Output
For each test case, print two lines with n integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between 1 and n, inclusive, and should satisfy the comparison results.

It can be shown that at least one answer always exists.

Example

inputCopy
3
3 <<
7 >><>><
5 >>><
outputCopy
1 2 3
1 2 3
5 4 3 7 2 1 6
4 3 1 7 5 2 6
4 3 2 1 5
5 4 2 1 3
Note
In the first case, 1 2 3 is the only possible answer.

In the second case, the shortest length of the LIS is 2, and the longest length of the LIS is 3. In the example of the maximum LIS sequence, 4 ‘3’ 1 7 ‘5’ 2 ‘6’ can be one of the possible LIS.
AC代码:
https://blog.csdn.net/qq_51392086/article/details/115642921utm_source=app&app_version=4.5.8
思维

第一行最大上升子序列最短的情况输出,第二行最大上升子序列最长的情况输出。
第一行要让大值尽量靠前,第二行要让小值尽量靠前。不只有这种思维,代码实践也很值得学习。

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;
int t,n;
int main() 
{
    string s;
    cin>>t;
    while(t--) 
    {
        cin>>n;
        cin>>s;
        int q=0;
        s[n-1]='>';//多标记一个主要符号
        for(int i=0;i<n;i++) 
        {
            if(s[i]=='>') //只考虑大于号即可,小于号时候的数在下面循环里就可以输出,很巧妙
            {
                for(int j=n-i;j<=n-q;j++)//上一个大于号到这个大于号这个区段里面需要的数字,如果中间隔有<,则隔了几个小于号,输出几个相对小的数。
                    cout<<j<<" ";
                q=i+1;//能帮助j找到,此时下一位置对应的最小值中的最大值,像长度是7,i=0输出7后,则q=1,下一位最大输出7-1=6;i=3,输出4,5后,则q=4,下一位置最大输出7-4=3。
            }
        }
        cout<<endl;
        //下段同理,尽量让大数去后面即可
        s[n-1]='<';
        q=0;
        for(int i=0;i<n;i++) 
        {
            if(s[i]=='<') 
            {
                for(int j=i+1;j>q;j--)
                    cout<<j<<" ";
                q=i+1;
            }
        }
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值