Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

不容易啊,滚动数组给我搞的纠结死了!终于ac了,代码给纠结的人们借鉴下

Posted by 501820158 at 2011-08-06 10:32:06 on Problem 1159
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator