ProgMe

Untitled

Apr 11th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool palindrom(string s)
  6. {
  7. int l = 0, r = s.size() - 1;
  8. bool ans = 1;
  9. while (l < r)
  10. {
  11. if (s [l] != s [r])
  12. ans = 0;
  13. l++;
  14. r--;
  15. }
  16. return ans;
  17. }
  18.  
  19. int main()
  20. {
  21. int n, m;
  22. cin >> n >> m;
  23. string s [n];
  24. for (int i = 0; i < n; i++)
  25. cin >> s [i];
  26. bool visited [n] = {};
  27. map <string, int> have;
  28. deque <string> ans;
  29. for (int i = 0; i < n; i++)
  30. {
  31. reverse(s [i].begin(), s [i].end());
  32. if (have [s [i]] != 0)
  33. {
  34. visited [have [s [i]] - 1] = 1;
  35. have [s [i]] = 0;
  36. ans.push_front(s [i]);
  37. reverse(s [i].begin(), s [i].end());
  38. ans.push_back(s [i]);
  39. visited [i] = 1;
  40.  
  41. }
  42. else
  43. {
  44. reverse(s [i].begin(), s [i].end());
  45. have [s [i]] = i + 1;
  46. }
  47. }
  48. int sz = ans.size() * m, it = -1;
  49. for (int i = 0; i < n; i++)
  50. {
  51. if (!visited [i])
  52. {
  53. if (palindrom(s [i]))
  54. {
  55. sz += m;
  56. it = i;
  57. break;
  58. }
  59. }
  60. }
  61. cout << sz << "\n";
  62. if (it == -1)
  63. {
  64. for (int i = 0; i < ans.size(); i++)
  65. cout << ans [i];
  66. }
  67. else
  68. {
  69. for (int i = 0; i < ans.size() / 2; i++)
  70. cout << ans [i];
  71. cout << s [it];
  72. for (int i = ans.size() / 2; i < ans.size(); i++)
  73. cout << ans [i];
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment