I am a beginner in C++, so it may be a stupid mistake. So, I wrote the following code to find longest palindrome in any word:
#include<bits/stdc++.h>
#include<string>
using namespace std;
bool IsPal(string str)
{
int length = str.length();
if (length < 1) return true;
else if (str.at(0) == str.at(length-1)) {str = str.substr(1, (length-2)); return IsPal(str);}
else return false;
}
int main()
{
int n;
cin >> n;
string x;
int *arRes = new int[n];
for (int i = 0; i < n; i++)
{
unsigned int maxim = 1;
cin >> x;
for (int j = 0; j < n; j++)
{
string sub = x.substr(0, j);
if(IsPal(sub))
{
if(sub.length() > maxim)
{ maxim = sub.length(); }
}
}
arRes[i] = maxim;
}
for (int i = 0; i < n; i++)
{
cout << arRes[i] << endl;
}
delete [] arRes;
}
And it doesn't pass this test:
8
woweffect
abccbaabc
testme
strstr
ababab
abcdefg
tetatet
aaaaaaaaaaaaa
It prints 3 6 1 1 5 1 7 7 instead of 3 6 1 1 5 1 7 13 and I can't understand why. Can someone explain it?