POJ 1159

将正序列和反序列做一次LCS就行

 

dp[i][j]表示正向到 i , 反向到 j 的LCS长度

 

dp[i][j]=max( dp[i-1][j-1]+1  //正向i==反向j

                      dp[i-1][j], dp[i][j-1] //正向i!=反向j )

 

最后要求的是len-dp[len][len]

 

#include <iostream> #include <string> #include <algorithm> #define F(i,a,b) for (int i=a;i<=b;i++) using namespace std; string str1, str2; short dp[5001][5001]; int main() { int len; cin >> len >> str1; str2=str1; reverse(&str2[0], &str2[len]); F(i,0,len) dp[i][0]=dp[0][i]=0; F(i,1,len) { F(j,1,len) { dp[i][j]=max(dp[i-1][j] , dp[i][j-1]); if (str1[i-1]==str2[j-1]) dp[i][j]=max( (int)dp[i][j], dp[i-1][j-1]+1); } } cout << len- dp[len][len] ; return 0; }

你可能感兴趣的:(BI,stdstring)