Skip to content

Instantly share code, notes, and snippets.

@sergiobuj
Created April 5, 2010 03:32
Show Gist options
  • Save sergiobuj/355976 to your computer and use it in GitHub Desktop.
Save sergiobuj/355976 to your computer and use it in GitHub Desktop.
Topcoder problem
Palidromized topcoder
string minAdds(string s) {
string res,a,b,aux;
a=s;b=s;
aux=s;
reverse(aux.begin(),aux.end());
if(s == aux)
return s;
for(int i=0; i<s.size(); i++){
string aux2 = a.substr(a.size()-1-i,i);
string aux3 = a.substr(0 , a.size()-i);
a=aux3+s[i]+aux2;
aux=a;
reverse(aux.begin(),aux.end());
if(a == aux)
break;
}
for(int i=0; i<s.size() ;++i){
string aux2 = s.substr(0,i);
reverse(aux2.begin(),aux2.end());
b=s+aux2;
aux=b;
reverse(aux.begin(),aux.end());
if(b == aux)
break;
}
return (a.size()<b.size())? a:b;
}
//g++ Palindromize.cc -Wall -g -o Palindromize && ./Palindromize
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment