-
Notifications
You must be signed in to change notification settings - Fork 0
/
B.cpp
65 lines (60 loc) · 1.3 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
bool pal(string str)
{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = str.length()-1;
// Keep comparing characters while they are same
while (h > l)
{
if (str[l++] != str[h--])
{
return false;
}
}
return true;
}
int main(){
fast;
ll n,m;
cin>>n>>m;
unordered_set<string> us;
string arr[n];
for(ll i=0;i<n;i++){
cin>>arr[i];
us.insert(arr[i]);
}
string ans;
for(ll i=0;i<n;i++){
if(!pal(arr[i])){
string temp(arr[i]);
reverse(temp.begin(),temp.end());
if(us.find(temp)!=us.end()){
ans += arr[i];
us.erase(temp);
us.erase(arr[i]);
}
}
}
ll mx=0;
string p;
for(string u: us){
if(pal(u)){
if(u.length()>mx){
mx = u.length();
p = u;
}
}
}
cout<<2*ans.length() + mx<<endl;
cout<<ans;
if(mx!=0)
cout<<p;
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
return 0;
}