-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
66 lines (59 loc) · 1.42 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
66
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(),v.end()
#define X first
#define Y second
#define rep(x, y, z) for(ll (z) = (x); (z) < (y); (z)++)
int constexpr N = 1e5;
int constexpr MOD = 1e9 + 7;
int constexpr INF = 1e9;
bool is_pali(string s) {
string ss = s;
reverse(all(ss));
return ss == s;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n, m;
cin >> n >> m;
map<string, int> cnt;
map<string, bool> isp;
rep (0, n, i) {
string s;
cin >> s;
cnt[s]++;
}
for (const auto& s : cnt) {
isp[s.X] = is_pali(s.X);
}
string ss, ess;
string bigp;
for (const auto& sss : cnt)
if (isp[sss.X] && (bigp.empty() || sss.X > bigp) && (cnt[sss.X] & 1)) bigp = sss.X;
map<string, bool> mark;
for (const auto& sss : cnt) {
if (mark[sss.X]) continue;
string rev = sss.X;
reverse(all(rev));
if(!isp[sss.X]) {
mark[rev] = true;
rep (0, min(sss.Y, cnt[rev]), i) {
ss += sss.X;
ess += sss.X;
}
}
else if (isp[sss.X] && sss.Y % 2 == 0) {
rep (0, sss.Y / 2, i) {
ss += sss.X;
ess += sss.X;
}
}
}
ss += bigp;
reverse(all(ess));
ss += ess;
cout << ss.size() << '\n';
cout << ss;
return 0;
}