fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_SIZE = 2000;
  6. const int LAST_LETTER = 'Z';
  7. const int CASE_DIFFERENCE = 'a' - 'A';
  8.  
  9. bool isIntresting(int fr[]) {
  10. for (int i = 'A'; i <= 'Z'; ++i) {
  11. if (fr[i] == 2) {
  12. return true;
  13. }
  14. }
  15. return false;
  16. }
  17.  
  18. void reverseWord(char text[], int startPos, int lastPos) {
  19. int halfPos = (lastPos + startPos - 1) / 2;
  20. for (int i = startPos; i <= halfPos; ++i) {
  21. char aux = text[i];
  22. text[i] = text[lastPos + startPos - i];
  23. text[lastPos + startPos - i] = aux;
  24. }
  25. }
  26.  
  27. bool isLetter(char c) {
  28. return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
  29. }
  30.  
  31. void resetToZero(int fr[]) {
  32. for (int i = 'A'; i <= 'Z'; ++i) {
  33. fr[i] = 0;
  34. }
  35. }
  36.  
  37. void countFrequency(int fr[], int c) {
  38. if ('A' <= c && c <= 'Z') {
  39. ++fr[c];
  40. return;
  41. }
  42. ++fr[c - CASE_DIFFERENCE];
  43. }
  44.  
  45. void transformText(char text[]) {
  46. int length = strlen(text), fr[LAST_LETTER + 1] = {0};
  47. int startPos = 0;
  48. bool prevLetter = false;
  49. for (int i = 0; i <= length; ++i) {
  50. if (isLetter(text[i])) {
  51. countFrequency(fr, text[i]);
  52. prevLetter = true;
  53. } else {
  54. if (prevLetter) {
  55. if (isIntresting(fr)) {
  56. reverseWord(text, startPos, i - 1);
  57. }
  58. resetToZero(fr);
  59. }
  60. prevLetter = false;
  61. startPos = i + 1;
  62. }
  63.  
  64. }
  65. }
  66.  
  67. int main() {
  68. char text[MAX_SIZE + 1];
  69. while (cin.getline(text, MAX_SIZE + 1)) {
  70. transformText(text);
  71. cout << text << '\n';
  72. }
  73. return 0;
  74. }
Success #stdin #stdout 0s 5296KB
stdin
Aabbc-aabb!!!
ramar
sS
stdout
cbbaA-bbaa!!!
ramar
Ss