Advertisement
Guest User

Untitled

a guest
Jan 1st, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4.  
  5. void parseInput(size_t& size, std::vector<std::string>& matrix) {
  6.   std::cin >> size;
  7.   std::string line;
  8.   for (size_t i = 0; i < size; ++i) {
  9.     std::cin >> line;
  10.     matrix.emplace_back(line);
  11.   }
  12. }
  13.  
  14. std::string matrixToString(const std::vector<std::string>& matrix) {
  15.   std::ostringstream oss;
  16.   for (auto const& line : matrix) {
  17.     oss << line << std::endl;
  18.   }
  19.   return oss.str();
  20. }
  21.  
  22. void initResult(const size_t& size, std::vector<std::string>& result) {
  23.   for (size_t i = 0; i < size; ++i) {
  24.     result.emplace_back(size, '.');
  25.   }
  26. }
  27.  
  28. void findGlitches(const size_t& size, const std::vector<std::string>& matrix, std::vector<std::string>& result) {
  29.   initResult(size, result);
  30.  
  31.   std::string symbols = "!?@#$%^&*()_+-=[]{}|:";
  32.  
  33.   bool done;
  34.   do {
  35.     done = true;
  36.     for (size_t i = 0; i < size; ++i) {
  37.       std::size_t col = matrix[i].find_first_of(symbols);
  38.       if (col != std::string::npos) {
  39.         done = false;
  40.         char symbol = matrix[i][col];
  41.         size_t row = 1;
  42.         while (matrix[i + row][col] == symbol) {
  43.           ++row;
  44.         }
  45.         row = i + row / 2;
  46.         result[row][col] = symbol;
  47.         symbols.erase(symbols.find(symbol), 1);
  48.         break;
  49.       }
  50.     }
  51.   } while (!done);
  52. }
  53.  
  54. int main() {
  55.   std::ios_base::sync_with_stdio(false);
  56.   std::cin.tie(nullptr);
  57.  
  58.   size_t size;
  59.   std::vector<std::string> matrix;
  60.   parseInput(size, matrix);
  61.  
  62.   std::vector<std::string> result;
  63.   findGlitches(size, matrix, result);
  64.  
  65.   std::cout << matrixToString(result);
  66.  
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement