| ||||||||||
| 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 | |||||||||
大家帮忙看看我的程序,有没有办法改进时间复杂性,老是超时#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator