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 ruanrongbin at 2005-04-07 16:58:17 on Problem 1159
#include<iostream.h>
char ch[5000];
int n;

//判断从数组下标从p到q的字符串构成回文要插入多少个字符
int judge(int p,int q)
{
	if(p>=q)return 0;
	if(ch[p]==ch[q])
		return judge(p+1,q-1);
    int x=judge(p,q-1)+1;
  int y=judge(p+1,q)+1;
  if(x<=y)return x;
    return y;


}

int main()
{

	int i;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>ch[i];
	cout<<judge(0,n-1)<<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