Hezov

Text OJI IX 2003

Jul 15th, 2025
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4. ifstream cin("text.in");
  5. ofstream cout("text.out");
  6. char para[1010];
  7. char *pCuv[1001];
  8. int main()
  9. {
  10.     int maxRand;
  11.     cin >> maxRand;
  12.     cin.get();
  13.     int nrPara = 0;
  14.     while(cin.getline(para,1005))
  15.     {
  16.         nrPara++;
  17.         if(strcmp(para,"") == 0)
  18.         {
  19.             cout << '\n';
  20.             continue;
  21.         }
  22.         int len = strlen(para);
  23.         char *p = strtok(para," ");
  24.         int nrCuv = 0;
  25.         while(p != NULL)
  26.         {
  27.             pCuv[++nrCuv] = p;
  28.             p = strtok(NULL," ");
  29.         }
  30.         if(nrCuv == 1)
  31.         {
  32.             cout << pCuv[nrCuv] << '\n';
  33.             for(int i = 0;i<=len;i++)
  34.                 para[i] = 0;
  35.             continue;
  36.         }
  37.         // Sa prelucram cuvintele si sa aflam nr de spatii lol
  38.         int pozC = 1, cLen = 1, sum = strlen(pCuv[1]);
  39.         while(pozC < nrCuv)
  40.         {
  41.             while(pozC < nrCuv && sum + strlen(pCuv[pozC+1]) + 1 <= maxRand)
  42.                 cLen++,sum+=strlen(pCuv[pozC+1]) + 1, pozC++;
  43.             int gaps = cLen - 1;
  44.             int totalSpaces = maxRand - sum + gaps;
  45.             int normalSpaces = totalSpaces / gaps;
  46.             int cntExtra = totalSpaces % gaps;
  47.             int cnt = 1;
  48.             if(pozC == nrCuv)
  49.             {
  50.                 for(int i = pozC-cLen+1;i<=pozC;i++)
  51.                 {
  52.                     cout << pCuv[i];
  53.                     if(i != pozC) cout << ' ';
  54.                 }
  55.                 cout << '\n';
  56.                 break;
  57.             }
  58.             for(int it = pozC - cLen + 1;it <= pozC;it++)
  59.             {
  60.                 cout << pCuv[it];
  61.                 if(it != pozC)
  62.                 {
  63.                    for(int j = 1;j<=normalSpaces;j++)
  64.                        cout << ' ';
  65.                    if(cnt <= cntExtra) cout << ' ' , cnt++;
  66.                 }
  67.                 else cout << ' ';
  68.             }
  69.             cout << '\n';
  70.             pozC++;
  71.             if(pozC!=nrCuv)
  72.                 cLen = 1, sum = strlen(pCuv[pozC]);
  73.             else cout << pCuv[nrCuv] << '\n';
  74.         }
  75.         for(int i = 0;i<=len;i++)
  76.             para[i] = 0;
  77.  
  78.     }
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment