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

求助 记忆化搜索怎么构造最优解啊..

Posted by uncleD at 2008-03-30 13:49:37 on Problem 1141
  刚刚开始用dp 不太熟悉..  不知道怎么在求解过程中构造最优解..  

  这个题 我求出了最少需要加多少个括号.. 但是不知道怎么记录添加括号的位置..

搜索函数:

int checkMsg(int head,int tail)//  (0,msgLen-1)
{
	int i;
	int temp;
	int posi =-1;
	int ans =MAX;
	
	if(head>tail)
		return 0;
	
	if(head==tail)
		return 1;

	if(proCount[head][tail]!=MAX)
		return proCount[head][tail];
	
	if( (oriMsg[head]=='('&&oriMsg[tail]==')')||
		(oriMsg[head]=='['&&oriMsg[tail]==']') )
	{
		if(proCount[head+1][tail-1]==MAX)
			proCount[head+1][tail-1] =checkMsg(head+1,tail-1);
		
		if(ans>proCount[head+1][tail-1])
			ans  =proCount[head+1][tail-1];
	}
	
	if(oriMsg[head]=='('||oriMsg[head]=='[')
	{
		if(proCount[head+1][tail]==MAX)
			proCount[head+1][tail] =checkMsg(head+1,tail);
		
		if(ans>(proCount[head+1][tail]+1))
		{
			ans  =proCount[head+1][tail]+1;
			posi =head;
		}
	}
	
	if(oriMsg[tail]==')'||oriMsg[tail]==']')
	{
		if(proCount[head][tail-1]==MAX)
			proCount[head][tail-1] =checkMsg(head,tail-1);
		
		if(ans>(proCount[head][tail-1]+1))
		{
			ans  =proCount[head][tail-1]+1;
			posi =tail;
		}
	}
	
	for(i=head;i<tail;i++)
	{
		temp =checkMsg(head,i)+checkMsg(i+1,tail);
		if(ans>temp) 
			ans =temp;
	}
	
	proCount[head][tail] =ans;
	addPosi[posi] =true;

	return proCount[head][tail];
	
}

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