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 matphyler at 2011-05-15 00:10:08 on Problem 1095
#include<iostream>
#include<string>
using namespace std;
int f[21]=
{
  1,1,2,5,14,
  42,132,429,1430,4862,
  16796,58786,208012,742900,2674440,
  9694845,35357670,129644790,477638700,1767263190,6564120420
};
string tree(int node,int num)
{
  string tr,tr_L,tr_R;
  int i,L,R;
  if(node==0)return tr;
  else if(node==1){tr.push_back('X');return tr;}
  for(i=0;i<node&&num>f[i]*f[node-1-i];num-=f[i]*f[node-1-i],i++);
  for(L=1;f[node-1-i]<num;num-=f[node-1-i],L++);
  R=num;
  tr_L=tree(i,L);
  tr_R=tree(node-i-1,R);
  if(tr_L.size())
  {
    tr.push_back('(');
    tr+=tr_L;
    tr.push_back(')');
  }
  tr.push_back('X');
  if(tr_R.size())
  {
    tr.push_back('(');
    tr+=tr_R;
    tr.push_back(')');
  }
  return tr;
}
int main()
{
  int num,i;
  cin>>num;
  while(num)
  {
    num++;
    for(i=0;num>f[i];num-=f[i],i++);
    cout<<tree(i,num)<<'\n';
    cin>>num;
  }
  return 0;
}



79MS

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