Created
April 5, 2010 03:32
-
-
Save sergiobuj/355976 to your computer and use it in GitHub Desktop.
Topcoder problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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