Recursion greatly minimizes the effort sometimes, like here in this question (on topcoder):- Thepalindrome
instead of any DP (although on the statistics page it was written to be dp ) a simple recursive approach can be applied :- algorithm/code
//header files
//class declarations
int find(string s)
{
int ret=rec(s);
return (s.size()-ret)+s.size();
}
int rec(string s)
{
string k=s;
std::reverse(k.begin(),k.end());
if(k==s)
{
return s.size();
}
else
{
s.erase(s.begin());
return rec(s);
}
}
and about the endsem of discrete maths i am not sure :P anyways gotta sleep now :)
instead of any DP (although on the statistics page it was written to be dp ) a simple recursive approach can be applied :- algorithm/code
//header files
//class declarations
int find(string s)
{
int ret=rec(s);
return (s.size()-ret)+s.size();
}
int rec(string s)
{
string k=s;
std::reverse(k.begin(),k.end());
if(k==s)
{
return s.size();
}
else
{
s.erase(s.begin());
return rec(s);
}
}
and about the endsem of discrete maths i am not sure :P anyways gotta sleep now :)
No comments:
Post a Comment