fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 2000;
  5. const int FIRST_LETTER = 'a';
  6. const int LAST_LETTER = 'z';
  7.  
  8. bool isFrequest(char currentLetter, char frequestLetter, int currentFrequency, int maxFrequency) {
  9. return frequestLetter == 0 || (currentFrequency > maxFrequency || (currentFrequency == maxFrequency && currentLetter > frequestLetter));
  10. }
  11.  
  12. bool isLowerLetter(char c) {
  13. return 'a' <= c && c <= 'z';
  14. }
  15.  
  16. void countLowerLetters(char text[], int fr[], char &frequestLetter, int maxFrequency) {
  17. for (int i = 0; text[i]; ++i) {
  18. if (isLowerLetter(text[i])) {
  19. ++fr[(int)text[i]];
  20. if (isFrequest(text[i], frequestLetter, fr[(int)text[i]], maxFrequency)) {
  21. frequestLetter = text[i];
  22. maxFrequency = fr[(int)text[i]];
  23. }
  24. }
  25. }
  26. }
  27.  
  28. bool existPalyndrome(int fr[]) {
  29. int numOfOddFrequencies = 0;
  30. for (int i = FIRST_LETTER; i <= LAST_LETTER; ++i) {
  31. if (fr[i] % 2 == 1) {
  32. ++numOfOddFrequencies;
  33. if (numOfOddFrequencies > 1) {
  34. return false;
  35. }
  36. }
  37. }
  38. return true;
  39. }
  40.  
  41. int main() {
  42. char text[MAX_SIZE + 1], frequestLetter = 0;
  43. int fr[LAST_LETTER + 1] = {0}, maxFrequency = 0;
  44. while (cin.getline(text, MAX_SIZE + 1)) {
  45. countLowerLetters(text, fr, frequestLetter, maxFrequency);
  46. }
  47. if (existPalyndrome(fr)) {
  48. cout << "DA";
  49. } else {
  50. cout << "NU";
  51. }
  52. cout << "\n" << frequestLetter;
  53. return 0;
  54. }
Success #stdin #stdout 0s 5276KB
stdin
a bb&Aa
stdout
DA
b