Advertisement
Sanlover

Untitled

Dec 22nd, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <Windows.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. pair<string*, size_t*> addNewWord(string* words, size_t* repeats, size_t& size, const string& newWord)
  8. {
  9. string* newWords = new string[size + 1];
  10. size_t* newRepeats = new size_t[size + 1];
  11. for (size_t i = 0; i < size; i++)
  12. {
  13. newWords[i] = words[i];
  14. newRepeats[i] = repeats[i];
  15. }
  16. newWords[size] = newWord;
  17. newRepeats[size] = 1;
  18. size++;
  19. return pair<string*, size_t*>(newWords, newRepeats);
  20. }
  21.  
  22. int main()
  23. {
  24. SetConsoleOutputCP(1251);
  25. SetConsoleCP(1251);
  26. ifstream input("input.txt");
  27. if (!input.is_open())
  28. {
  29. cout << "File not found";
  30. return 0;
  31. }
  32. string* words = nullptr;
  33. size_t* repeats = nullptr;
  34. size_t amount = 0;
  35. while (input.good())
  36. {
  37. string tmp;
  38. getline(input, tmp);
  39. const pair<string*, size_t*> response = addNewWord(words, repeats, amount, tmp);
  40. words = response.first;
  41. repeats = response.second;
  42. }
  43. input.close();
  44.  
  45. for (int i = 0; i < amount - 1; i++) {
  46. for (int j = 0; j < amount - i - 1; j++) {
  47. if (repeats[j] > repeats[j + 1]) {
  48. swap(words[j], words[i]);
  49. swap(repeats[j], repeats[i]);
  50. }
  51. }
  52. }
  53. for(size_t i =0 ; i < amount; i++)
  54. {
  55. cout << words[i] << endl;
  56. }
  57. return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement