Recently brush several times of OJ, good hurt many are similar topic.
Longest palindrome substring
stringPreprocess (string&STR) {stringAfterprocessstr="#"; for(intI=0; I<str.size (); ++i) {afterprocessstr + = Str.substr (i,1)+"#"; }returnAFTERPROCESSSTR;//afterprocessstr.clear ();}intMaxpalindrome (string&STR) {stringAfterprocessstr=preprocess (str);//cout<<afterprocessstr<<endl; intMaxedge=0, center=0;int*p=New int[Afterprocessstr.size ()] ();intans=0;intCur=0; for(intI=1; I<afterprocessstr.size (); ++i) {p[i]= (maxedge>i) min (maxedge-i,p[2*center-i]):0; while(I-1-p[i]>=0&&i+1+p[i]<afterprocessstr.size () &&afterprocessstr[i+1+p[i]]==afterprocessstr[i-1-p[i]] ++p[i];if(I+p[i]>maxedge) {center=i; Maxedge=i+p[i]; }if(P[i]>ans) ans=p[i]; }returnAns;}
Note that the Preprocess function above will take a significant amount of time, preferably with pre-allocated memory.
staticstring afterProcessStr(1000002*2,‘#‘);
See details: http://blog.csdn.net/zhouyelihua/article/details/46964175
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
OJ Template Gallery