Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool palindrom(string s)
- {
- int l = 0, r = s.size() - 1;
- bool ans = 1;
- while (l < r)
- {
- if (s [l] != s [r])
- ans = 0;
- l++;
- r--;
- }
- return ans;
- }
- int main()
- {
- int n, m;
- cin >> n >> m;
- string s [n];
- for (int i = 0; i < n; i++)
- cin >> s [i];
- bool visited [n] = {};
- map <string, int> have;
- deque <string> ans;
- for (int i = 0; i < n; i++)
- {
- reverse(s [i].begin(), s [i].end());
- if (have [s [i]] != 0)
- {
- visited [have [s [i]] - 1] = 1;
- have [s [i]] = 0;
- ans.push_front(s [i]);
- reverse(s [i].begin(), s [i].end());
- ans.push_back(s [i]);
- visited [i] = 1;
- }
- else
- {
- reverse(s [i].begin(), s [i].end());
- have [s [i]] = i + 1;
- }
- }
- int sz = ans.size() * m, it = -1;
- for (int i = 0; i < n; i++)
- {
- if (!visited [i])
- {
- if (palindrom(s [i]))
- {
- sz += m;
- it = i;
- break;
- }
- }
- }
- cout << sz << "\n";
- if (it == -1)
- {
- for (int i = 0; i < ans.size(); i++)
- cout << ans [i];
- }
- else
- {
- for (int i = 0; i < ans.size() / 2; i++)
- cout << ans [i];
- cout << s [it];
- for (int i = ans.size() / 2; i < ans.size(); i++)
- cout << ans [i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment