题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
C++代码,算法效率较高:
#include<iostream> #include<string> using namespace std; int main() { string str1; while(cin>>str1) { int len1=str1.length(); string str; for(int i=0;i<len1;++i) { if(isalnum(str1[i])) str+=str1[i]; } int len=str.length(); int index=0; int index1=0,index2=0; for(int i=0;i<len;++i) { index1=0; index2=1; int m=i,n=i+1; while(str[m]==str[n]&&n<len&&m>=0) { index1+=2; m--; n++; } int a=i,b=i+2; while(str[a]==str[b]&&b<len) { index2+=2; a--; b++; } if(index1>index) index=index1; if(index2>index) index=index2; } cout<<index<<endl; } return 0; }