//输入字符串
//输出最长子序列长度
1 int longestSubSeq(string ss) 2 { 3 int n=(int)ss.size(); 4 if(n==0) 5 { 6 return 0; 7 } 8 //申请辅助内存 9 int **f=new int* [n]; 10 for(int k=0;k<n;++k) 11 { 12 f[k]=new int[n]; 13 } 14 //初始化 15 for(int i=0;i<n;++i) 16 { 17 f[i][i]=1; 18 if(i<n-1) 19 { 20 if(ss[i]==ss[i+1]) 21 { 22 f[i][i+1]=2; 23 } 24 } 25 } 26 //计算顺序 27 int len; 28 int Max=1; 29 for(len=3;len<=n;++len) 30 for(int i=0;i<=n-len;++i) 31 { 32 int j=i+len-1; 33 f[i][j]=max(f[i+1][j],f[i][j-1]); 34 if(ss[i]==ss[j]) 35 { 36 f[i][j]=max(f[i][j],f[i+1][j-1]+2); 37 if(f[i][j]>Max) 38 { 39 Max=f[i][j]; 40 } 41 } 42 } 43 //释放内存 44 for (int l = 0; l <n; ++l) { 45 delete [] f[l]; 46 } 47 delete []f; 48 return Max; 49 }