usaco--heritage
2012-11-27 14:54
253 查看
纯属蒙过去的。汗一个。。。。程序有bug..
ifstream fin("heritage.in");
ofstream fout("heritage.out");
char io[maxn],po[maxn];
struct node
{
char ch;
node *left,*night;
};
node *newnode()
{
node *u=new node;
u->left=u->night=null;
u->ch='9';
return u;
}
node *root;
void init()
{
fin>>io>>po;
root=newnode();
}
void build(node *root,int li,int ri,int lp,int rp)
{
if(li>ri||lp>rp)
{
//cout<<"cao!"<<endl;
return;
}
if(li==ri)
{
root->ch=io[li];
return;
}
char ch=po[lp];
int pos;
for(int i=li;i<=ri;i++)
if(ch==io[i])
{
root->ch=ch;
pos=i;
break;
}
node *temp=newnode();root->left=temp;
build(temp,li,pos-1,lp+1,lp+pos-li);
temp=newnode();root->night=temp;
build(temp,pos+1,ri,lp+pos-li+1,rp);
}
void out(node *root)
{
if(root==null)return;
out(root->left);
out(root->night);
if(root->ch!='9')
fout<<root->ch;
delete root;
}
void slove()
{
int len=strlen(io);
len--;
build(root,0,len,0,len);
out(root);
}
int main()
{
init();
slove();
fout<<endl;
return 0;
}
ifstream fin("heritage.in");
ofstream fout("heritage.out");
char io[maxn],po[maxn];
struct node
{
char ch;
node *left,*night;
};
node *newnode()
{
node *u=new node;
u->left=u->night=null;
u->ch='9';
return u;
}
node *root;
void init()
{
fin>>io>>po;
root=newnode();
}
void build(node *root,int li,int ri,int lp,int rp)
{
if(li>ri||lp>rp)
{
//cout<<"cao!"<<endl;
return;
}
if(li==ri)
{
root->ch=io[li];
return;
}
char ch=po[lp];
int pos;
for(int i=li;i<=ri;i++)
if(ch==io[i])
{
root->ch=ch;
pos=i;
break;
}
node *temp=newnode();root->left=temp;
build(temp,li,pos-1,lp+1,lp+pos-li);
temp=newnode();root->night=temp;
build(temp,pos+1,ri,lp+pos-li+1,rp);
}
void out(node *root)
{
if(root==null)return;
out(root->left);
out(root->night);
if(root->ch!='9')
fout<<root->ch;
delete root;
}
void slove()
{
int len=strlen(io);
len--;
build(root,0,len,0,len);
out(root);
}
int main()
{
init();
slove();
fout<<endl;
return 0;
}
相关文章推荐
- USACO/heritage 3.4.2 前序 中序 后序
- usaco3.4.1 American Heritage
- USACO / American Heritage (经典递归入门)
- USACO American Heritage
- JZOJ1294.【USACO题库】3.4.2 American Heritage美国血统
- USACO 3.4.3 American Heritage
- USACO 3.4 American Heritage
- USACO3.4.1 American Heritage (heritage)
- USACO3.4.2--American Heritage
- USACO 3.4 American Heritage (heritage)
- usaco-3.4-heritage-passed
- USACO3.4.1-heritage
- usaco American Heritage
- usaco American Heritage
- [USACO3.4.1]heritage
- USACO 3.4 American Heritage
- USACO Training 3.4.2 American Heritage 题解与分析
- USACO American Heritage
- American Heritage usaco
- USACO-Section 3.4 American Heritage (分治)