fork download
  1. #ifndef _GLIBCXX_NO_ASSERT
  2. #include <cassert>
  3. #endif
  4. #include <cctype>
  5. #include <cerrno>
  6. #include <cfloat>
  7. #include <ciso646>
  8. #include <climits>
  9. #include <clocale>
  10. #include <cmath>
  11. #include <csetjmp>
  12. #include <csignal>
  13. #include <cstdarg>
  14. #include <cstddef>
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <ctime>
  19.  
  20. #if __cplusplus >= 201103L
  21. #include <ccomplex>
  22. #include <cfenv>
  23. #include <cinttypes>
  24. #include <cstdbool>
  25. #include <cstdint>
  26. #include <ctgmath>
  27. #include <cwchar>
  28. #include <cwctype>
  29. #endif
  30.  
  31. // C++
  32. #include <algorithm>
  33. #include <bitset>
  34. #include <complex>
  35. #include <deque>
  36. #include <exception>
  37. #include <fstream>
  38. #include <functional>
  39. #include <iomanip>
  40. #include <ios>
  41. #include <iosfwd>
  42. #include <iostream>
  43. #include <istream>
  44. #include <iterator>
  45. #include <limits>
  46. #include <list>
  47. #include <locale>
  48. #include <map>
  49. #include <memory>
  50. #include <new>
  51. #include <numeric>
  52. #include <ostream>
  53. #include <queue>
  54. #include <set>
  55. #include <sstream>
  56. #include <stack>
  57. #include <stdexcept>
  58. #include <streambuf>
  59. #include <string>
  60. #include <typeinfo>
  61. #include <utility>
  62. #include <valarray>
  63. #include <vector>
  64.  
  65. #if __cplusplus >= 201103L
  66. #include <array>
  67. #include <atomic>
  68. #include <chrono>
  69. #include <condition_variable>
  70. #include <forward_list>
  71. #include <future>
  72. #include <initializer_list>
  73. #include <mutex>
  74. #include <random>
  75. #include <ratio>
  76. #include <regex>
  77. #include <scoped_allocator>
  78. #include <system_error>
  79. #include <thread>
  80. #include <tuple>
  81. #include <typeindex>
  82. #include <type_traits>
  83. #include <unordered_map>
  84. #include <unordered_set>
  85. #endif
  86.  
  87. using namespace std;
  88.  
  89. #define ms(s, n) memset(s, n, sizeof(s))
  90. #define FOR(i, a, b) for (int i = (a); i < (b); i++)
  91. #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
  92. #define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
  93. #define sz(a) int((a).size())
  94. #define all(a) (a).begin(), (a).end()
  95. #define uni(a) (a).erase(unique(all(a)), (a).end())
  96. #define pb push_back
  97. #define pf push_front
  98. #define mp make_pair
  99. #define fi first
  100. #define se second
  101. #define prec(n) fixed<<setprecision(n)
  102. #define bit(n, i) (((n) >> (i)) & 1)
  103. #define bitcount(n) __builtin_popcount(n)
  104. typedef long long ll;
  105. typedef unsigned long long ull;
  106. typedef long double ld;
  107. typedef pair<int, int> pi;
  108. typedef vector<int> vi;
  109. typedef vector<pi> vii;
  110. const int MOD = (int) 1e9 + 7;
  111. const int INF = (int) 1e9;
  112. const ll LINF = (ll) 1e18;
  113. const ld PI = acos((ld) -1);
  114. const ld EPS = 1e-9;
  115. ll gcd(ll a, ll b) {if(a<b)swap(a,b);ll r; while (b) {r = a % b; a = b; b = r;} return a;}
  116. ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
  117. ll fpow(ll n, ll k, int p = MOD) {ll r = 1;for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
  118. template<class T> void setmin(T& a, T val) {if (a > val) a = val;}
  119. template<class T> void setmax(T& a, T val) {if (a < val) a = val;}
  120. void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
  121. void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
  122. int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
  123. int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
  124.  
  125. string mulstring(string s, int count){
  126. string r = "";
  127. FOR(i,0,count)
  128. r += s;
  129. return r;
  130. }
  131.  
  132. string rev(string s){
  133. string r(s);
  134. reverse(r.begin(), r.end());
  135. return r;
  136. }
  137.  
  138. int main(){
  139. int n, m;
  140. cin >> n >>m;
  141. unordered_map<string, int> freq;
  142. FOR(i,0,n){
  143. string s;
  144. cin >> s;
  145. freq[s]++;
  146. }
  147. string prefix = "";
  148. string middle = "";
  149. for(auto x: freq){
  150. string s = x.first;
  151. int count_s = x.second;
  152. string r = rev(s);
  153. if(s==r){
  154. prefix += mulstring(s,(count_s/2));
  155. if(middle=="" && count_s%2)
  156. middle = s;
  157. }
  158. else {
  159. int count_r = freq[r];
  160. // cout << r << " " << count_r << endl;
  161. prefix += mulstring(s, min(count_s, count_r));
  162. }
  163. freq[s] = 0;
  164. freq[r] = 0;
  165. }
  166. string suffix = rev(prefix);
  167. string ans = prefix + middle + suffix;
  168. cout << ans.size() << "\n" << ans << "\n";
  169. return 0;
  170. }
Success #stdin #stdout 0s 4548KB
stdin
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
stdout
20
zyxwbabaijjiababwxyz