AtCoder ABC 159

Here is my code for task B String Palindrome (B - String Palindrome):-

    #include <bits/stdc++.h>
    using namespace std;
     
    #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define rep(i,a,b) for(int i=a; i<b; i++)
    #define ll long long
    #define pb push_back
    #define inf 1e+18L
    #define MOD 1000000007
     
    bool isPalin(string s, int i, int j)
    {
        for(int x=i, y=j; x != y; x++, y--)
        {
            if(s[x] != s[y]) return false;
        }
        return true;
    }
     
    int main()
    {
        FastIO
        string s;
        cin >> s;
        int n = s.size();
        if(isPalin(s,0,n-1))
        {
            if(isPalin(s,0,(n-1)/2 - 1))
            {
                if(isPalin(s,(n+3)/2 - 1, n-1))
                {
                    cout << "Yes\n";
                }
                else cout << "No\n";
            }
            else cout << "No\n";
        }
        else cout << "No\n";
        return 0;
    }

It gives AC on 4 test cases. What is wrong in this code? Can anyone please help?

Consider the test case

aaaaa

I get No, which I know is wrong. But why does this happen?

Try putting

cout<<x<<" "<<y<<"\n";

In your for loop.
Also consider making you code more readable

Your code, but beautified and fixed
#include <bits/stdc++.h>
using namespace std;

#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,a,b) for(int i=a; i<b; i++)
#define ll long long
#define pb push_back
#define inf 1e+18L
#define MOD 1000000007

bool isPalin(string s, int i, int j){
    for(int x=i, y=j; x < y; x++, y--){
        if(s[x] != s[y]){ 
            return false;
        }
    }
    return true;
}

int main(){
    FastIO
    string s;
    cin >> s;
    int n = s.size();
    int x=(n-1)/2;
    if(isPalin(s,0,n-1) && isPalin(s,0,x - 1) && isPalin(s,x+1, n-1)){
        cout<<"Yes\n";
    }
    else{ 
        cout <<"No\n";
    }
    return 0;
}
1 Like

Thank you so much !