Longest Palindrome
Longest Palindrometime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputReturning 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 nn distinct strings of equal length mm. 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.InputThe first line contains two integers nn and mm (1≤n≤100, 1≤m≤50) — the number of strings and the length of each string.Next nn lines contain a string of length mm each, consisting of lowercase Latin letters only. All strings are distinct.OutputIn 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
Input
3 3
tab
one
bat
Output
6
tabbat
题意:
给出n给长m的字符串,每个都不相同,求旋转字符串仍然是自己的最长字符串长度和其中的一个字符串。
题解
旋转字符串为自己的字符串又两个情况:
1。自己就是旋转字符串,
2.可以找到自己的旋转字符串
#include <bits/stdc++.h>
using namespace std;
char a[1000001];
long long ans = 0;
int main() {
int n, m;
cin >> n >> m;
set<string> A;
string s[103];
for(int i = 0; i < n; i++) {
cin >> s[i];
A.insert(s[i]);
}
string ans;
string mid;
for(int i = 0; i < n; i++) {
string a = s[i];//不可以写成 string a = reverse(s[i].begin(), s[i].end());
reverse(a.begin(), a.end());
if(a == s[i])
mid = a;
else if(A.find(a) != A.end()) {
ans += a;
A.erase(s[i]);
}
}
int len = ans.length() * 2 + mid.length();
cout << len << endl;
string ans2 = ans;
reverse(ans2.begin(), ans2.end());
cout << ans << mid << ans2 << endl;
}