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.
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.
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.
3 3
tab
one
bat
6
tabbat
4 2
oo
ox
xo
xx
6
oxxxxo
3 5
hello
codef
orces
0
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
20
ababwxyzijjizyxwbaba
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.
删除一些字符串,并重新排列剩下的字符串,使得到的字符串是回文,并且字符串的长度要尽可能的长。
我做的时候没有注意到每个字符串都是不同的,所以把字符串可能相同的情况也考虑进去了,就显得比较复杂。我是先用map记录字符串的个数,然后找是否有自己就是回文的,如果有,则把这个字符串放入list序列中,并将map中这个字符串的个数减1,然后找是否存在一个字符串,它的正序和反序对应的map值都不为0的,若有,则将这个字符串的正序和反序一前一后地加入到list序列中,最后按顺序输出即可。
(emmm,现在想想自己写的还真复杂)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=5050;
map<string,int> mp,st;
list<string> l;
list<string>::iterator it;
struct node{
int id;
string s;
}f[maxn];
string Reverse(string d,int m)
{
for(int i=0,j=m-1;i<j;i++,j--){
swap(d[i],d[j]);
}
return d;
}
int main()
{
int n,m;
cin>>n>>m;
mp.erase(mp.begin(),mp.end());
st.erase(st.begin(),st.end());
for(int i=0;i<n;i++){
string a;
cin>>a;
mp[a]+=1;
f[i].id=i;
f[i].s=a;
}
for(int i=0;i<n;i++){
if(mp[f[i].s]%2==1&&f[i].s==Reverse(f[i].s,m)){
l.push_back(f[i].s);
mp[f[i].s]--;
break;
}
}
for(int i=0;i<n;i++){
string b=f[i].s;
string c=Reverse(b,m);
if(mp[b]!=0&&mp[c]!=0&&(b!=c||(b==c&&mp[b]>1))){
l.push_front(b);
l.push_back(c);
mp[b]--;
mp[c]--;
}
}
int flag=0;
if(l.size()==0){
flag=1;
}
cout<<l.size()*m<<endl;
for(it=l.begin();it!=l.end();it++){
cout<<*it;
}
if(flag==0)
cout<<endl;
return 0;
}
大佬的解法,比我的简单多了。
#include
using namespace std;
set<string>st;
int main()
{
int n,m;
string s,t,p="",ans="";
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>s,t=s;
reverse(t.begin(),t.end());
if(s==t) p=t;
else if(st.count(t)) ans+=t;
st.insert(s);
}
string r=ans;
reverse(r.begin(),r.end());
ans=ans+p+r;
cout<<ans.size()<<endl<<ans<<endl;
}