/***Compute the longest palindrome substring ***/
#include <iostream>
#include <string>
using namespace std;
bool isPalindrom(string str, int start, int end) {
if (start == end || start > end) return true;
while (start != end && start < end)
{
if (str[start] != str[end]) return false;
else {
start += 1;
end -= 1;
}
}
return true;
}
string chk_longPalindrom(string str, int start, int chklen) {
string outstr;
int st;
while (chklen != 0)
{
st = start;
for(unsigned int i = 0; i < str.size()-chklen+1; i++) {
if (isPalindrom(str, st, st+chklen-1))
{
outstr.assign(str.begin()+st, str.begin()+st+chklen);
return outstr;
}
st++;
}
chklen–;
}
return “”;
}
void main() {
string str = “FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth”;
string longsub = chk_longPalindrom(str, 0, str.size());
cout<<“Long sub string is [ “<<longsub<<” ]”<<endl;
}