fork download
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define ll long long
  5. using namespace std;
  6. signed main() {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10. // 9th grade
  11. string s;
  12. int n;
  13. cin>>n>>s;
  14. map<char, int> mp;
  15. for(int i = 0; i < s.size(); i++) {
  16. mp[s[i]]++;
  17. }
  18. set<char> st;
  19. st.insert(s.begin(), s.end());
  20. char c;
  21. int cnt = 0;
  22. for(char i : st) {
  23. if(mp[i]%2!=0) {
  24. c=i;
  25. cnt++;
  26. }
  27. }
  28. if(cnt!=1) {
  29. cout<<"NOT POSSIBLE\n";
  30. return 0;
  31. }
  32. if(st.size()==1) {
  33. for(int i = 0; i < s.size()/2; i++) {
  34. cout<<s[i];
  35. }
  36. return 0;
  37. }
  38. int g = 0;
  39. bool ok=0;
  40. for(int i = 0; i < s.size(); i++) {
  41. if(s[i]==c&&!ok) {ok=1;g=i;break;}
  42. }
  43. if(g==0&&s[s.size()-1]==c) {
  44. cout<<"NOT UNIQUE\n";
  45. } else {
  46. string s1, s2;
  47. for(int i = 0; i < s.size(); i++) {
  48. if(i<s.size()/2)
  49. s1+=s[i];
  50. else
  51. s2+=s[i];
  52. }
  53. int i = 0, j = 0;
  54. while(true) {
  55. if(s1[i]==s2[j]) {
  56. i++, j++;
  57. } else if(s1[i]!=s2[j]) {
  58. j++;
  59. }
  60. if(i>=s1.size()||j>=s2.size()) break;
  61. }
  62. if(i==s1.size()) {
  63. cout<<s1<<'\n';
  64. return 0;
  65. }
  66. s1="", s2="";
  67. for(int i = 0; i < s.size(); i++) {
  68. if(i<s.size()/2+1)
  69. s1+=s[i];
  70. else
  71. s2+=s[i];
  72. }
  73. i = 0, j = 0;
  74. while(true) {
  75. if(s2[i]==s1[j]) {
  76. i++, j++;
  77. } else if(s2[i]!=s1[j]) {
  78. j++;
  79. }
  80. if(i>=s2.size()||j>=s1.size()) break;
  81. }
  82. if(i==s2.size()) {
  83. cout<<s2<<'\n';
  84. return 0;
  85. }
  86. cout<<"NOT POSSIBLE\n";
  87. }
  88. // end of code
  89. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
NOT POSSIBLE