uva 712 S-Trees

//水题,直接模拟

 

#include <stdio.h>
#include <string.h>
int string[10],x[10],level;
int numnode[10]={1,2,4,8,16,32,64,128};
char leaf[300];
int main()
{
    int T=0,n,m,i,j,p,q,num,level;  char ch,temp[10],ans[300];
    while(scanf("%d",&n)!=EOF && n)
    {
        getchar(); T++;  printf("S-Tree #%d:\n",T);
        for(i=0; i<n; i++)  scanf("%c%d%c",&ch,&string[i],&ch);  //接住空格,回车符

        for(q=0,i=0;i<=n; i++) q+=numnode[i]; p=q-numnode[n]+1;  
//printf("%d     %d\n",p,q);
        
        for(i=p; i<=q; i++)     scanf("%c",&leaf[i]); leaf[i]='\0'; getchar();
        scanf("%d",&m);  getchar();
        for(j=0; j<m; j++)
        {
            scanf("%s",temp+1); for(i=1; i<=n; i++)  x[i]=temp[i]-'0';
            num=1; level=0;
            while(level<n)
            {
                i=string[level];
                if(x[i]) num=num*2+1;
                else     num=num*2;
                level++;
            }
            ans[j]=leaf[num];
        }
        ans[j]='\0'; printf("%s\n\n",ans);
    }
    return 0;
}

 

 

posted @ 2012-10-05 22:54  Titanium  阅读(218)  评论(0编辑  收藏  举报