# Palindrome Pain ,Please someone check this is not working

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

int main() {
int t;
cin>>t;
while(t–)
{
int a,b;
cin>>a>>b;
if((a%2&&b%2))
{
cout<<“-1”<<endl;
continue;
}
else if(a==1||b==1)
{
cout<<“-1”<<endl;
continue;
}
else if(a%2==0&&b%2==0)
{
int x=min(a,b);
int rem=abs(a-b);
string a=“”,b=“”;
while(x)
{
a+=‘a’;
b=‘a’+b;
a+=‘b’;
b=‘b’+b;
x-=2;
}
while(a>b&&rem>0)
{
a+=‘a’;
rem–;
}
while(b>a&&rem>0)
{
b=‘b’+b;
rem–;
}
string t1=a+b;
string t2=t1;
int i=1;
while(i<t2.size())
{
swap(t2[i],t2[i-1]);
i+=2;
}
cout<<t2<<endl;
cout<<t1<<endl;
}
else
{
if(a%2)
{
a–;
int x=min(a,b);
int rem=abs(a-b);
string a=“”,b=“”;
while(x)
{
a+=‘a’;
b=‘a’+b;
a+=‘b’;
b=‘b’+b;
x-=2;
}
while(a>b&&rem>0)
{
a+=‘a’;
b=‘a’+b;
rem-=2;
}
while(b>a&&rem>0)
{
b=‘b’+b;
a+=‘b’;
rem-=2;
}
string t1=a+‘a’+b;
string t2=t1;
reverse(t2.begin(),t2.begin()+t2.size()/2);
reverse(t2.begin()+t2.size()/2+1,t2.end());
cout<<t2<<endl;
cout<<t1<<endl;
}
else
{
b–;
int x=min(a,b);
int rem=abs(a-b);
string a=“”,b=“”;
while(x)
{
a+=‘a’;
b=‘a’+b;
a+=‘b’;
b=‘b’+b;
x-=2;
}
while(a>b&&rem>0)
{
a+=‘a’;
b=‘a’+b;
rem-=2;
}
while(b>a&&rem>0)
{
b=‘b’+b;
a+=‘b’;
rem-=2;
}

``````       string t1=a+'b'+b;
string t2=t1;
reverse(t2.begin(),t2.begin()+t2.size()/2);
reverse(t2.begin()+t2.size()/2+1,t2.end());
cout<<t2<<endl;
cout<<t1<<endl;
}

}
}
return 0;
``````

}

@mohdkaif123

Sure,I am attaching problem link here:PALINPAIN Problem - CodeChef
please let me know where i am going wrong.It will help a lot.

@mohdkaif123
Your code is too long to debug .
I have written with almost same logic .
plzz refer this solution for better understanding.

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

int main() {
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
char ch='p';
if(a%2&&b%2)
{
cout<<-1<<endl;
continue;
}
else if(a==1||b==1)
{
cout<<-1<<endl;
continue;
}
else if(a%2)
{
ch='a';
a--;
}
else if(b%2)
{
ch='b';
b--;
}
a=a/2;
b=b/2;
string s1,s2;
for(int i=0;i<a;i++)
{
s1+='a';
}
for(int i=0;i<b;i++)
s1+='b';
string tm=s1;
reverse(s1.begin(),s1.end());
s2=s1;
s1=tm;
if(ch!='p')
s1+=ch;
s1+=s2;
if(ch!='p')
s2+=ch;
s2+=tm;
cout<<s1<<endl;
cout<<s2<<endl;
}
return 0;
}
``````
1 Like

Thanks a lot!! could you plz tell me how should i solve the qustions so that in contest time
.question could be solved.

just try to solve the question rated 100 above of your comfort zone .
Keep practice bro .