Advertisement
wowonline

Untitled

Mar 19th, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. enum { MAX_SIZE = 32768 };
  7.  
  8.  
  9. // bool comp(char *p1, char *p2)
  10. // {
  11. //     return std::strcmp(p1, p2) < 0;
  12. // }
  13.  
  14. int main()
  15. {
  16.     char s[MAX_SIZE], c;
  17.     int i = 0, size;
  18.  
  19.     while ((std::cin.get(c)) && !std::cin.eof()) {
  20.         s[i++] = c;
  21.     }
  22.  
  23.     //truncatig
  24.     size = std::strlen(s); // will be actual size of truncated string
  25.     for (int i = size - 1; i >= 0; --i) {
  26.         if (s[i] == ' ') {
  27.             size--;
  28.         } else {
  29.             break;
  30.         }
  31.     }
  32.  
  33.     char *ptrs[size];
  34.     for (int i = 0; i < size; ++i) {
  35.         ptrs[i] = s + sizeof(int) * i;
  36.     }
  37.  
  38.     for(int i = 0; i < size; ++i) {
  39.         std::cout<<ptrs[i]<<std::endl;
  40.     }
  41.    
  42.     std::sort(ptrs, ptrs+size, [](char *p1, char *p2) {
  43.         return (std::strcmp(p1, p2)) < 0;
  44.     });
  45.  
  46.     for (int i = 0; i < size; ++i) {
  47.         std::cout << (ptrs[i] - s)/sizeof(int) << std::endl;
  48.     }
  49.     // for(int i = 0; i < size; ++i) {
  50.     //     std::cout<<ptrs[i]<<std::endl;
  51.     // }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement