简体   繁体   中英

Why am I getting a WA in Faded Palindromes in Codechef even though I found no error till now in my code and its working fine for me?

I have been debugging and seeing this code for nearly a week now still I couldn't find any solution and thus hoping to get one from the fellow developers in stack overflow.

This is the solution of the Faded Palindrome problem of September Challenge Codechef This is the problem page

#include<iostream>
#include<vector>
#include<iterator>
#include<stdlib.h>
#include<cstdio>
#include<string>

using namespace std;
string def="-1";
int main()
{
    int t;
    cin>>t;
    //vector<string> out(t);
    string out[100];
    for(int i=0;i<t;i++)
    {
        string inp;
        cin>>inp;
        if(inp.size()%2)   //Checking wether the number of letters is odd
        {
            int counter=inp.size()/2;
            int counter_back=counter-1;
            int counter_front=counter+1;
            if(inp[counter]=='.')
            inp[counter]='a';
            int not_fa=0;
            while(counter_back>=0&&counter_front<inp.size())
            {
                if(inp[counter_back]==inp[counter_front])
                  {counter_back--;counter_front++;}
                else if(inp[counter_back]=='.'&&inp[counter_front=='.']){
                inp[counter_back]=inp[counter_front]='a';
                counter_back--;counter_front++;
                }
                else if(inp[counter_back]=='.'){
                inp[counter_back]=inp[counter_front];
                counter_back--;counter_front++;
                }
                else if(inp[counter_front]=='.'){
                inp[counter_front]=inp[counter_back];
                counter_back--;counter_front++;
                }
                else{
                    not_fa=1;
                    break;
                }
            }
            if(not_fa)
               out[i]=def;
            else
                out[i]=inp;
        }
        else{      //Checking the number of letter is even
            //cout<<inp.size();
            int counter_front=(inp.size())/2;
            //cout<<counter_front;
            int counter_back=counter_front-1;
            //cout<<counter_back;
            int not_fa=0;
            while(counter_back>=0&&counter_front<inp.size())
            {
                if(inp[counter_back]==inp[counter_front])
                  {//cout<<"*-";
                  counter_back--;counter_front++;}
                else if(inp[counter_back]=='.'&&inp[counter_front]=='.'){
                inp[counter_back]=inp[counter_front]='a';
                counter_back--;counter_front++;
                }
                else if(inp[counter_back]=='.'){
                //cout<<"--";
                inp[counter_back]=inp[counter_front];
                counter_back--;counter_front++;
                }
                else if(inp[counter_front]=='.'){
                inp[counter_front]=inp[counter_back];
                counter_back--;counter_front++;
                }
                else{
                    not_fa=1;
                    break;
                }
            }
            if(not_fa)
                out[i]=def;
            else
                out[i]=inp;

        }
    }
    /*vector<string>::iterator itr;
    for(itr=out.begin();itr!=out.end();itr++)
        cout<<*itr<<endl;*/
    for(int j=0;j<t;j++)
    if(j==t-1)
        cout<<out[j];
    else
        cout<<out[j]<<"\n";


    return 0;
}

While your code may pass all the test cases presented in the problem statement, there are still more edge cases that your code doesn't cover. Here's a sample test case:

6
..
...
..e
a..a.v.
p..p
t.xzt

This should print:

aa
aaa
eae
avaaava
paap
tzxzt

instead it prints:

..
.a.
aaa
aa.a.aa
p..p
taxat 

Hints:

  1. The first if statement is not correctly handling the case where inp[b] and inp[f] are both '.' .
  2. There is no need for doing two different things when |s| is even or odd.
  3. Also, in the first else if statement, you probably meant inp[counter_front]=='.' instead of inp[counter_front=='.'] .

if(inp[counter_back]==inp[counter_front]) is true if they both are equal to '.' so you won't fill that gap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM