Advertisement
dmkozyrev

408.cpp

Jun 6th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. string trim(const string & s) {
  8.     int l = 0, r = (int) s.size()-1;
  9.     while (isspace(s[l])) ++l;
  10.     while (isspace(s[r])) --r;     
  11.     return s.substr(l, r-l+1);
  12. }
  13.  
  14. int main() {
  15.     setlocale(LC_ALL, ".1251");
  16.    
  17.     freopen("input.txt", "rt", stdin);
  18.     freopen("output.txt", "wt", stdout);
  19.    
  20.     int k, n;
  21.     scanf("%d %d\n", &k, &n);
  22.    
  23.     vector<string> v(n);
  24.    
  25.     bool flag = 1;
  26.    
  27.     for (auto & s : v) {
  28.         getline(cin, s);
  29.         s = trim(s);
  30.         flag &= s.size() <= k;
  31.     }
  32.    
  33.     if (flag) {
  34.         for (int i = 0; i < n; ++i ){
  35.             int countSpaces = k - (int)v[i].size();
  36.             int leftSpaces = countSpaces / 2;
  37.             int rightSpaces = countSpaces - leftSpaces;
  38.             if (i) cout << endl;
  39.             cout << string(leftSpaces, ' ')+v[i]+string(rightSpaces, ' ');
  40.         }
  41.     } else {
  42.         cout << "Impossible.";
  43.     }
  44.    
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement