Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不容易啊,滚动数组给我搞的纠结死了!终于ac了,代码给纠结的人们借鉴下#include <iostream> #include <string> using namespace std; #define max(a, b) ((a) > (b) ? (a) : (b)) #define N 5001 int dp[N][2]; int main () { char str1[N], str2[N]; int n; while(cin >> n) { cin >> str1; int i, j; for(i = 0, j = n-1; i < n&& j >= 0; i++, j--) str2[j] = str1[i]; memset(dp, 0, sizeof(dp)); for(j = 1; j <= n; j++) { for(i = 1; i <= n; i++) { if(str1[i-1] == str2[j-1]) dp[i][j%2] = dp[i-1][(j+1)%2] + 1; else dp[i][j%2] = max(dp[i][(j+1)%2], dp[i-1][j%2]); } } cout << n - dp[i-1][(j+1)%2] << endl; } return 0; } // Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator