UVa 712

1.用完全二叉树

#include <stdio.h>
int order[10];
int fulltree[2000];
int ans[10];
char str[2000];
int calculate(char* str, int n)
{
    int i;
    int p = 0;
    for(i = 0; i < n; i++)
    {
        if( str[order[i]-1] == '0') p = p * 2 + 1;
        else p = p * 2 + 2;
    }
    return fulltree[p];
}


int main()
{
    int cas = 0;
    while(1)
    {
        cas++;

        int n, m, i;
        char temp;
        char vva[10];
        memset(ans, 0, sizeof(ans));
        scanf("%d", &n);
        memset(order, 0, sizeof(order));
        memset(fulltree, 0, sizeof(fulltree));
        getchar();
        if(!n) break;
        for(i = 0; i < n; i++)
        {
            scanf("%c%d",&temp, &order[i]);
            getchar();
        }
        scanf("%s", str);
        for(i = (1<<n) - 1; i < (2<<n); i++)
            fulltree[i] = str[i-(1<<n) + 1] - '0';
        scanf("%d", &m);
        for(i = 0; i < m; i++)
        {
            scanf("%s", vva);
            ans[i] = calculate(vva, n);
        }
        printf("S-Tree #%d:\n",cas);
        for(i = 0; i < m; i++)
            printf("%d", ans[i]);
        printf("\n\n");
    }
    return 0;
}


另一种是用二进制的思想,即为:把目标数列按照数的处理顺序排好形成的二进制串对应的十进制即为相应的答案的下标

主要函数如下:

void f(string s, string s1, vector<int> floor) {
    int t = 1, k = 0;
    for(int i = floor.size()-1; i >= 0; --i)
    {
        k += (s1[floor[i]-1] - '0') * t;
        t *= 2;
    }
    cout << s[k];
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值