Search code examples
c++mathpalindrome

Faded Palindromes,why i'm getting wrong verdict?


Faded characters in the string are represented by '.' where as other characters are lower case Latin alphabets i.e ['a'-'z'].one or more faded character string can be consist. i want construct the lexicographically smallest palindrome by filling each of the faded character ('.') with a lower case Latin alphabet.I'm trying 3 hours more but i getting wrong verdict. I can't fix my problem. Can anyone help me?

#include<bits/stdc++.h>
using namespace std;

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    int test;
    string s;
    cin>>test;
    while(test--)
    {
        cin>>s;
        for(int i=0; i<s.size()/2; i++)
        {
            if(s[i]=='.')
            {
                if(s[i]==s[s.size()-1-i])
                {
                    s[i]='a';
                    s[s.size()-1-i]='a';
                }
            }
        }
        if(s.size()%2) s[s.size()/2]='a';
        //cout<<"s = "<<s<<endl;


        for(int i=0; i<s.size()/2; i++)//left string checking
        {
            if(s[i]=='.')
            {
                s[i]=s[s.size()-1-i];
            }
        }

        for(int i=s.size()-1,k=0; i>s.size()/2; i--,k++)//Right string checking
        {
            if(s[i]=='.')
            {
                s[i]=s[k];
            }
        }

        //cout<<" ss = "<<s<<endl;

        bool flag=true;
        for(int i=0; i<s.size()/2; i++)
        {
            if(s[i]!=s[s.size()-1-i])
            {
                flag=false;
            }
        }

        if(flag==false) cout<<"-1"<<endl;
        else cout<<s<<endl;

    }
}

IDEOne link


Solution

  • Correction Line ::66 you put middle character always 'a'.We should put character 'a' when middle position are faded character. May be help you.