Goat Research - Easy Problems - Solution to Toph.co Online Judge Problem

Goat Research

Limits 1s, 512 MB
Goats are interesting creatures. They are incredibly durable and can fall from great heights, be struck by moving vehicles, or even explode without being harmed. Goats are heralds of chaos, and leave destruction in their wake. Don’t feel bad about it; it’s just the nature of being a goat.
There are many kinds of goats that don’t look like goats, but don’t be fooled, because as the saying goes,“If it’s not a human, it’s a goat.”
You can learn more about Goats from the Official Goat Simulator wiki.
Goats are very particular about the sounds they make. Since it’s something difficult for us humans to understand, we usually simplify the goat’s sound to mere “baaaa”.
You have been observing goats for a very long time and have made transcripts of their conversations. You have written it all down in your diary.
You are afraid that a goat may see your notes through their rectangular pupils of 320-degree peripheral vision. (Yes, goats have rectangular pupils, but you have bigger problems at hand.) And so, you want to re-write your notes in a way that pleases the goats.
Given a series of lines of “baaaa’s” you have to ensure that each line adheres to the following conditions:
  • If the number of a’s is not even, make it even by removing an a.
  • If the number of a’s would become zero because of removing one a, add an a instead to make the number of a’s even.
  • Add spaces to the beginning of each line to ensure that the baaaa’s are aligned to the center. (Do not add any extra spaces at the end of the lines.)

Input

The input will begin with an integer N (N < 20). The following N lines will contain “baaaa’s” of varying lengths. Each line will always have exactly one b at the beginning and a number of a’s following it (at least 1). The length of a line will never exceed 20 characters.

Output

Print the lines in the same order, but after applying the changes that would make the transcripts pleasing to the goats.

Sample


InputOutput
5
baaaaa
baaa
baaaaaaa
baaaaaaaaaa
baa
baaaa
    baa
  baaaaaa
baaaaaaaaaa
    baa

Solution:
#include<iostream> using namespace std; int main() { int n,i,j; cin>>n; string s[n]; for(i=0;i<n;i++) cin>>s[i]; int maxLen = s[0].size(); for(i=0;i<n;i++) { int l = s[i].size(); if(l>maxLen) maxLen = l; } for(i=0;i<n;i++) { int len = s[i].size(); int t = len; len-=1; if(len%2==1) { if(len-1==0) len+=1; else len-=1; } for(j=0;j<(maxLen-len-1)/2;j++) cout<<" "; cout<<"b"; for(j=0;j<len;j++) cout<<"a"; cout<<endl; } }

Post a Comment

0 Comments