ACM学习历程—广东工业大学2016校赛决赛-网络赛D 二叉树的中序遍历(数据结构)

题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=3

这算是一个胡搞类型的题目。当然肯定是有其数据结构支撑的。

唯一的限制就是不能出现连续的两个’#’。

因为如果我从左到右构造这棵树,那么假设我构造到第i个,

如果i+1是数字,那么把前i个构成的子树作为i+1的左儿子即可。

如果i+1’#’,那么把’#’当成i的右儿子即可。

所以只要没有两个连续的’#’,自然能通过上面的方法构造。

但是如果出现两个连续的’#’,自然前一个’#’是后一个’#’的前继,说明后一个’#’存在左儿子或者前一个’#’存在右儿子,自然矛盾了。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long

using namespace std;

const int maxN = 100005;
char str[maxN];

void work()
{
    int len = strlen(str), cnt = 0;
    for (int i = 0; i < len; ++i)
    {
        if (str[i] == '#') cnt++;
        if (cnt == 2)
        {
            printf("no
");
            return;
        }
        if (str[i] != '#') cnt = 0;
    }
    printf("yes
");
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 1; times <= T; ++times)
    {
        scanf("%s", str);
        work();
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/andyqsmart/p/5375189.html