5B - Center Alignment - CodeForces Solution


implementation strings *1200

Please click on ads to support us..

Python Code:

import sys
a=[]
mx=0
while True:
    try:
        i=input()
        
        start=0
        end=len(i)-1
        while i and i[start]==' ':
            start+=1
        while i and i[end]==' ':
            end-=1
        mx=max(mx,end-start+1)
        a.append(i[start:end+1])
        
        
    except:
        break
print('*'*(mx+2))
c=0

for i in a:
    print('*',end='')
    if (mx-len(i))%2==0:
        print(' '*((mx-len(i))//2),end='')
        
        print(i,end='')
        print(' '*((mx-len(i))//2),end='')
    elif (mx-len(i))%2!=0 and c%2==0:
        print(' '*((mx-len(i))//2),end='')
        print(i,end='')
        print(' '*(((mx-len(i))-((mx-len(i))//2))),end='')
        c+=1
    elif (mx-len(i))%2!=0 and c%2==1:
        print(' '*(((mx-len(i))-((mx-len(i))//2))),end='')
        print(i,end='')
        print(' '*((mx-len(i))//2),end='')
        
        
        c+=1
    
    print('*')

print('*'*(mx+2))


        

C++ Code:

// Hydro submission #6411678186e4667c8fe2e831@1678862210157
#include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
string t[1005];
int n = 1, maxlen;
bool delta;
int main()  {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    char ch;    int l = 0;
    do  {
        l = 0;
        do  {
            ch = getchar();
            if(ch == '\n' || ch == EOF) break;
            t[n].insert(t[n].end(), 1, ch);
        }   while(ch != '\n' && ch != EOF);
        ++n;
    }
    while(ch != EOF);
    --n;
    --n;
    //cout << strlen(s[1]) << endl;
    for(int i = 1; i <= n; ++i) if(t[i].length() > maxlen)   maxlen = t[i].length();

    for(int i = 1; i <= maxlen + 2; ++i)    cout << "*";

    cout << endl;
    for(int i = 1; i <= n; ++i) {
        cout << "*";
        if((maxlen & 1) != (t[i].length() & 1)) {
            for(int j = 1; j <= (maxlen - t[i].length()) / 2 + delta; ++j)  cout << " ";
            cout << t[i];
            for(int j = 1; j <= (maxlen - t[i].length()) / 2 + !delta; ++j) cout << " ";
            delta = !delta;
        }
        else    {
            for(int j = 1; j <= (maxlen - t[i].length()) / 2; ++j) cout << " ";
            cout << t[i];
            for(int j = 1; j <= (maxlen - t[i].length()) / 2; ++j) cout << " ";
        }
        cout << "*" << endl;
    }
    for(int i = 1; i <= maxlen + 2; ++i)    cout << "*";
    cout << endl;
}


Comments

Submit
0 Comments
More Questions

1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number