OJ模板库
近期刷了好幾次的oj,好受傷好多都是相似的題目。
最長回文子串
string preprocess(string &str) {string afterProcessStr="#";for(int i=0;i<str.size();++i){afterProcessStr += str.substr(i, 1)+"#";}return afterProcessStr;//afterProcessStr.clear(); } int maxpalindrome(string &str) {string afterProcessStr=preprocess(str);// cout<<afterProcessStr<<endl;int maxEdge=0,center=0;int *p=new int[afterProcessStr.size()]();int ans=0;int cur=0;for(int i=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];}return ans; }注意上文中preprocess函數會花費大量時間最好是採用預分配內存。
static string afterProcessStr(1000002*2,'#');詳細見:http://blog.csdn.net/zhouyelihua/article/details/46964175
最大公約數
常常使用的最大公約數的方法有輾轉相除法
/* 輸入x,y 返回x,y的最大公約數 */ int gcd(int x,int y) { if(x<y)return gcd(y,x); if(0==y)return x; else {if(0==x&0x1){if(0==y&0x01)return (gcd(x>>1,y>>1)<<1);elsereturn gcd(x>>1,y);}else{if(0==y&0x01)return gcd(x,y>>1);elsereturn gcd(y,x-y);} } }總結
- 上一篇: Mysql性能调优工具Explain结合
- 下一篇: [HDU3336]Count the s