Advertisement
Guest User

Untitled

a guest
Dec 31st, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using String = std::string;
  9.  
  10. template <typename T>
  11. using Vector = std::vector<T>;
  12.  
  13. using StringVector = Vector<String>;
  14. using IntVector = Vector<int>;
  15. using CharVector = Vector<char>;
  16.  
  17.  
  18.  
  19. template <typename T>
  20. std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
  21.     switch (v.size()) {
  22.     case 0:
  23.         return out << "[]";
  24.     case 1:
  25.         return out << "[" << v.front() << "]";
  26.     default:
  27.         out << "[" << v.front();
  28.         for (u_int i = 1; i < v.size(); i++)
  29.             std::cout << ", " << v[i];
  30.         return out << "]";
  31.     }
  32. }
  33.  
  34. String readFile(String filename) {
  35.     std::ifstream       file(filename);
  36.     std::stringstream   buffer;
  37.  
  38.     if (!file.is_open())
  39.         throw std::runtime_error("Error: file not open!");
  40.  
  41.     buffer << file.rdbuf();
  42.     return buffer.str();
  43. }
  44.  
  45. StringVector split(String str, String separators, bool removeSeparator=true) {
  46.     StringVector    strs;
  47.     String          tmp;
  48.     size_t          i = 0, j = 0;
  49.  
  50.     while (i < str.size()) {
  51.         if (separators.find(str[i]) == String::npos) {
  52.             i++;
  53.             continue;
  54.         }
  55.         tmp = str.substr(j, i - j + !removeSeparator);
  56.         if (!removeSeparator or not tmp.empty())
  57.             strs.push_back(tmp);
  58.         j = ++i;
  59.     }
  60.     if (j < i) {
  61.         if (!removeSeparator or not tmp.empty())
  62.             strs.push_back(str.substr(j, i - j + !removeSeparator));
  63.     }
  64.     return strs;
  65. }
  66.  
  67. IntVector intoRepresentation(String str) {
  68.     IntVector   repr;
  69.     int         digit, tmp;
  70.  
  71.     for (size_t i = 0, id = 0; i < str.size(); i++) {
  72.         digit = (int) (str[i] - 48);
  73.         tmp = (i % 2 == 0) ? id++ : -1;
  74.         while (digit-- > 0)
  75.             repr.push_back(tmp);
  76.     }
  77.  
  78.     return repr;
  79. }
  80.  
  81. u_int find_empty_of(const IntVector &repr, const u_int &size) {
  82.     u_int i = 0, j;
  83.     while (i < repr.size()) {
  84.         while (i < repr.size() and repr[i] != -1)
  85.             i++;
  86.         j = i;
  87.         while (j < repr.size() and repr[j] == -1)
  88.             j++;
  89.         if (j - i >= size)
  90.             return i;
  91.         i++;
  92.     }
  93.     return -1;
  94. }
  95.  
  96. int main(int ac, char *av[]) {
  97.     String          text = (ac != 2) ? readFile("example.txt") : String(av[1]);
  98.     IntVector       repr = intoRepresentation(text);
  99.     const IntVector org(repr.begin(), repr.end());
  100.     u_int           i, j, h;
  101.  
  102.     std::cout << repr << std::endl;
  103.    
  104.     i = 0;
  105.     j = repr.size() - 1;
  106.     while (j < repr.size()) {
  107.         while (j < repr.size() and j > 0 and repr[j] == -1) j--;
  108.         h = j;
  109.         do {
  110.             j--;
  111.         } while (j < repr.size() and j > 0 and (repr[j] == repr[j + 1]));
  112.  
  113.         if (j >= repr.size())
  114.             break;
  115.  
  116.         // check if num has been already moved
  117.         if (org[h] != repr[h])
  118.             continue;
  119.  
  120.         // find empty space of enough size
  121.         i = find_empty_of(repr, h - j);
  122.         if (i > j)
  123.             continue;
  124.  
  125.         // move the sequence into the empty space
  126.         while (h > j) {
  127.             repr[i++] = repr[h];
  128.             repr[h--] = -1;
  129.         }
  130.     }
  131.  
  132.     std::cout << repr << std::endl;
  133.  
  134.     u_int count = 0; // calculate checksum
  135.     for (i = 0; i < repr.size(); i++) {
  136.         if (repr[i] != -1)
  137.             count += i * repr[i];
  138.     }
  139.  
  140.     std::cout << "The filesystem checksum is " << count << std::endl;
  141.     return (0);
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement