American Heritage usaco

基础题,主要思路是找到根,然后分别递归即可;

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 using namespace std;
 7 char s[30],t[30];
 8 int n;
 9 void dfs(int x,int y,int a,int b){
10     int k;
11     for(int i=a;i<=b;i++)if(s[i]==t[x]){k=i;break;}
12     if(k!=a)dfs(x+1,x+k-a,a,k-1);
13     if(k!=b)dfs(x+k-a+1,y,k+1,b);
14     cout<<t[x];
15 }
16 void work(){
17     scanf("%s%s",s,t);
18     n=strlen(s);
19     dfs(0,n-1,0,n-1);
20 }
21 int main(){
22     freopen("1.in","r",stdin);
23     freopen("1.out","w",stdout);
24     work();
25 }
View Code

 

posted @ 2016-09-02 13:42  CHADLZX  阅读(155)  评论(0编辑  收藏  举报