关闭

UVA 297 - Quadtrees

340人阅读 评论(0) 收藏 举报
分类:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define maxn 20000+10
using namespace std;
struct node
{
    int data, flag;
    node *ll, *l, *r, *rr;
};
int tmpi;
node* &build(int cur, char *s)
{
    node *t;
    t = (node*)malloc(sizeof(node));
    t->data = 1024/(int)pow(4, cur);
    if(s[tmpi] == 'p')
    {
        t->flag = 0;
        ++tmpi;
        t->ll = build(cur+1, s);
        ++tmpi;
        t->l = build(cur+1, s);
        ++tmpi;
        t->r = build(cur+1, s);
        ++tmpi;
        t->rr = build(cur+1, s);
    }
    else if(s[tmpi] == 'e')
    {
        t->flag = 1;
        t->ll = NULL;
        t->l = NULL;
        t->r = NULL;
        t->rr = NULL;
    }
    else
    {
        t->flag = 2;
        t->ll = NULL;
        t->l = NULL;
        t->r = NULL;
        t->rr = NULL;
    }
    return t;
}
void dfs(node *t, int &sum)
{
    if(t == NULL) return;
    if(t->flag == 2)
        sum+=t->data;
    dfs(t->ll, sum);
    dfs(t->l, sum);
    dfs(t->r, sum);
    dfs(t->rr, sum);
    return;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        char s1[2000], s2[2000];
        scanf("%s%s", s1, s2);
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        int len = 0;
        char s3[4000];
        int st1 = 0, st2 = 0;
        while(st2 < len1 && st2 < len2)
        {
            if(s1[st1] == s2[st2]) s3[len++] = s1[st1++], ++st2;
            else
            {
                if(s1[st1] == 'f')
                {
                    s3[len++] = s1[st1++];
                    if(s2[st2] == 'e')
                        ++st2;
                    else{
                        ++st2;
                        int tmp = 4;
                        for(int i = 0; i < tmp; ++i)
                        {
                            if(s2[st2] != 'p')
                                ++st2;
                            else tmp+=4, ++st2;
                        }
                    }
                }
                else if(s2[st2] == 'f')
                {
                    s3[len++] = s2[st2++];
                    if(s1[st1] == 'e')
                        ++st1;
                    else{
                        ++st1;
                        int tmp = 4;
                        for(int i = 0; i < tmp; ++i)
                        {
                            if(s1[st1] != 'p')
                                ++st1;
                            else tmp+=4, ++st1;
                        }
                    }
                }
                else if(s1[st1] == 'p')
                {
                    ++st2;
                    s3[len++] = s1[st1++];
                    int tmp = 4;
                    for(int i = 0; i < tmp; ++i)
                    {
                        if(s1[st1] == 'p') tmp += 4;
                        s3[len++] = s1[st1++];
                    }
                }
                else if(s2[st2] == 'p')
                {
                    s3[len++] = s2[st2++];
                    int tmp = 4;
                    for(int i = 0; i < tmp; ++i)
                    {
                        if(s2[st2] == 'p') tmp += 4;
                        s3[len++] = s2[st2++];
                    }
                    ++st1;
                }
            }
        }
        while(st1 < len1) s3[len++] = s1[st1++];
        while(st2 < len2) s3[len++] = s2[st2++];
        s3[len] = '\0';
        node *tree;
        tmpi = 0;
        tree = build(0, s3);
        int ans = 0;
        dfs(tree, ans);
        printf("There are %d black pixels.\n", ans);
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:64586次
    • 积分:2244
    • 等级:
    • 排名:第16823名
    • 原创:165篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论