zoukankan      html  css  js  c++  java
  • hdu 1622 Trees on the level

    题意:按层次遍历二叉树

    思路:对于不会建树的同学,直接广搜即可

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <map>
    #include <string>
    #include <queue>
    using namespace std;
    #define maxn 300
    char s[maxn];
    int d[maxn],cnt,n;
    map<string,int>mp;
    bool bfs()
    {
        string str="";
        queue<string>q;
        q.push(str);
        if(mp[str]) d[cnt++]=mp[str];
        else return false;
        while(!q.empty())
        {
            str=q.front();
            q.pop();
            if(mp[str+'L'])
            {
                d[cnt++]=mp[str+'L'];
                q.push(str+'L');
            }
            if(mp[str+'R'])
            {
                d[cnt++]=mp[str+'R'];
                q.push(str+'R');
            }
        }
        if(cnt==n) return true;
        return false;
    }
    void init()
    {
        n=0;
        cnt=0;
        mp.clear();
    }
    int main()
    {
        int i,j,k,m,len;
        init();
        while(scanf(" %s",s)!=EOF)
        {
            if(strcmp(s,"()")==0)
            {
                if(!bfs()) printf("not complete
    ");
                else
                {
                    for(i=0;i<cnt;i++)
                    {
                        printf("%d",d[i]);
                        if(i==cnt-1) printf("
    ");
                        else printf(" ");
                    }
                }
                init();
                continue;
            }
            n++;
            j=len=strlen(s);
            m=0;
            for(i=1; i<len; i++)
            {
                if(s[i]==',')
                {
                    j=i+1;
                    break;
                }
                m=m*10+s[i]-'0';
            }
            string str="";
            for(;j<len-1;j++)
                str+=s[j];
            mp[str]=m;
        }
        return 0;
    }
  • 相关阅读:
    React之React.cloneElement
    HTB-靶机-Vault
    HTB-靶机-Curling
    HTB-靶机-Zipper
    HTB-靶机-Frolic
    HTB-靶机-Carrier
    HTB-靶机-Oz
    HTB-靶机-Dab
    HTB-靶机-Waldo
    HTB-靶机-Reddish
  • 原文地址:https://www.cnblogs.com/zuferj115/p/5432934.html
Copyright © 2011-2022 走看看