Goat Research

Goats are interesting creatures. They are incredibly durable and can fall from great heights, be str…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

all output match…but not accepted.

#include<iostream>
using namespace std;

int main()
{

	int n;
	string s[20];
	cin >> n;
	if (n < 20)
	{

		for (int i = 0; i < n; i++)
		{
			string q;
			cin >> q;
			int f = q.length();
			if (f <= 20)
			{
				if (q.at(0) == 'b')
				{
					int z = 0;
					int k = 0;
					for (int i = 1; i < f; i++)
					{
						if (q[i] == 'a')
						{
							z++;
						}
						else if (q[i] != 'a')
						{
							k++;
						}

					}
					if (z >= 1 && k == 0)
					{
						if (z == 1)
						{

							for (int i = 0; i <= f; i++)
							{
								if ((i+1) == f)
								{
								    q.resize(f+1);
									q.at(i+1) = 'a';
								}

							}
							s[i] = q;
						}



						else if (z > 1)
						{
						    if(z%2==1)
                            {
							f - 1;
							for (int i = 0; i < f; i++)
							{
								if (i == (f - 1))
								{
									q[i] = NULL;
								}

							}
							s[i] = q;
                            }
                            else
                              {
                                s[i]=q;
                              }

						}


					}



				}
			}
		}

		int xx = 0;
		int maxlen = 0;
		for (int i = 0; i < n; i++)
		{
			if (s[i].empty()==false)
			{
				xx = s[i].length();
				if (xx > maxlen)
				{
					maxlen = xx;
				}

			}


		}
		int maxdev = 20 - maxlen;
		if (maxdev >= 0)
		{
			maxdev = maxdev / 2;
			for (int i = 0; i < n; i++)
			{
				int size = 0;
				size = s[i].length();
				int div = 20 - size;
				if (div >= 0)
				{
					div = div / 2;
					div = div - maxdev;
					for (int i = 0; i < div; i++)
					{
						cout << " ";

					}
					cout << s[i] << endl;
				}
				else
                {
                    cout<<s[i];
                }

			}

		}
	}







}

Don’t know why but your code is printing some NoN-ASCII characters at the end.
see this robinak47's Problem - Diff Checker .

1 Like

thanks vai…i’ve solved the problem.and finally it accepted.thanks a lot vai

vai,i got WA in test case 2. test case dile upokar hoto.
my code:

#include<bits/stdc++.h>
#include<list>
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdin)

#define ll long long int
#define PI acos(-1)
#define MAX 1000000007

using namespace std;

vector< int >vec;
list<char >l;
list<char >::iterator it;

int main()
{
   /// fread;
   // fwrite;
    int n;
    cin>>n;
    while(n--)
    {
        string s;int i=0;
        cin>>s;
        while(s[i]!='\0')
        {
            l.push_back(s[i++]);
        }
       // for(it = l.begin();it!=l.end();it++)cout<<*it<<endl;
        int si = l.size();

        if(si==1)
        {
            l.push_back('a');
            l.push_back('a');
        }
        else if(si==2)l.push_back('a');
        else if(si%2)
        {

        }
        else
        {
            l.reverse();
            it = l.begin();
            l.erase(it);
            l.reverse();
        }
        if(si==1 || si==2 || si == 3 || si == 4){
                it=l.begin();
            l.insert(it,' ');
            l.insert(it,' ');
            l.insert(it,' ');
            l.insert(it,' ');
        }
        else if(si==6 || si == 5)
        {
            it=l.begin();
            l.insert(it,' ');
            l.insert(it,' ');
            l.insert(it,' ');
        }
        else if(si==7 || si == 8)
        {
            it=l.begin();
            l.insert(it,' ');
            l.insert(it,' ');
        }
        else if(si==9 || si == 10)
        {
            it=l.begin();
            l.insert(it,' ');
        }
        for(it = l.begin();it!=l.end();it++)
        {
            cout<<*it;
        }
        l.clear();
        cout<<endl;
    }
    return 0;
}
times = int(input())
lis = list()
for i in range(1,times +1):
    string = input()
    index = string.rfind("a")
    if index%2 !=0:
        nstring = string[0:index]
        nindex = nstring.rfind("a")
        if(nindex == -1):
           lis.append("ba")
        else:
            lis.append(nstring)
    else:
        lis.append(string)
maxl = len(max(lis,key=len))
for x in lis:
    
    
    print(x.center(maxl))

my code gives right answer in my editor but does not pass the tests. can anyone explain where am I moving wrong?

Welcome to the Toph Community, @mchoden!

You’re using the center() method to align the string.

But, the Python center() method adds characters to both ends of a string to align it to the center. So no matter which character you use, the final string will always have trailing fill characters (change the character parameter to something else to visualize easily).
So, you have to do the aligning part manually, or somehow remove the trailing spaces.

for T in range(int(input())):
    s = ' ' + input()
    c = s.count('a')
    if c == 1:
        print(s+'a')
    elif c % 2 == 0:
        print(s)
    else:
        print(s[:-1])

Why WA? :roll_eyes:

@utsaroy because you need to align them properly

2 Likes
n=int(input())
if 0<n<20:
    listOfBaa= []
    maxLen=0
    for i in range(n):
        x =input()
        if(maxLen<len(x)):
            maxLen=len(x)
            listOfBaa.append(x)
        elif(len(x)<=20):
            listOfBaa.append(x)
    for i in range(len(listOfBaa)):
        baa=list(listOfBaa[i])
        if len(baa)==2:
            baa.append('a')
        elif(len(baa)%2==0):
            baa.pop(len(baa)-1)
        outputResult=''.join(baa)
        print(' '*(int(maxLen/2)-int(len(outputResult)/2))+outputResult)

Why? input validation is not necessary. :slightly_smiling_face:

1 Like

Looks like you have solved the problem. Nice work!

When a problem statement gives you a constraint around the input, it is there to help you understand how you should be solving the problem and how the test cases have been prepared. You do not need to validate it yourself in your code. You can assume that the constraints given will be maintained in the test case.

In the unfortunate circumstance where the constraints are incorrect, it is upon the author of the problem to correct it.

1 Like

thank you, sir. I had done it by a little change. but it was accepted by both (validated input and unvalidated input).

Solution in python3 :

[REDACTED]

I solved it in python3. so

Why I’m getting RTE in test case 3, can anyone help?

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n, temp, len;
    cin >> n;

    string cases[n];

    for(int i = 0; i < n; i++){
        cin >> cases[i];
    }

    len = 0;
    for(int i = 0; i < n; i++){
        temp = cases[i].size();
        if(temp > len){
            len = cases[i].size();
        }
    }

    for(int i = 0; i < n; i++){
        if(cases[i].size() % 2 == 0){
            if(cases[i].size() == 2){
                cases[i] = cases[i] + 'a';
            }
            else{
                cases[i].pop_back();
            }
        }

        temp = (len - cases[i].size()) / 2;

        cases[i] = string(temp, ' ') + cases[i];
        // cases[i].insert(0, temp, ' ');

        cout << cases[i] << "\n";
    }

    return 0;
}