zoukankan      html  css  js  c++  java
  • 偶遇递归树

    递归树

    st = "aaa-kkk-(bbbb#llll-ewwww#ccc-hhhh#yyyy-(ddd-ggg#eee))"
    
    class AAAA:
        def str_dict(self,data):
            # 数据处理
            data_dict = {}
            if type(data) == str:
                data_dict['ddt'] = data
                return data_dict
            else:
                data_list = []
                for i in data:
                    data_dict['ddt'] =i
                    data_list.append(data_dict)
                    data_dict={}
                return data_list
        def fenge(self, lis1, l):
            # 递归树的深层递归方法
            print(lis1)
            in1 = lis1.index("#")
            in2 = lis1.index("(")
            if in1 < in2:
                lis2 = lis1.split("#", 1)
                if "-" in lis2[0]:
                    lis3 = lis2[0].split("-")
                    dd = self.str_dict(lis3)
                    l.append(dd)
                else:
                    dd = self.str_dict([lis2[0]])
                    l.append(dd)
    
                self.fenge(lis2[1], l)
            else:
                l.append(self.dd(lis1, []))
    
        def dd(self, st, l):
            # 平层次递归方法
            if "(" not in st:
                if "#" not in st:
                    lis3 = st.split("-")
                    return lis3
                else:
                    liswww = st.split("#")
                    for item in liswww:
                        dd = self.str_dict(item.split('-'))
                        l.append(dd)
            else:
                lis = st.split("(", 1)
                #  lis= "bbbb-sss#ccc-hhhh#kkk-(ddd-ggg#eee))"
                ll = self.str_dict(item for item in lis[0].split("-"))
                for i in ll:
    
                    l.append(i)
                print(lis)
                if '#' in lis[1]:
                    if "(" in lis[1]:
                        self.fenge(lis[1], l)
                    else:
                        liswww = lis[1].split("#")
                        for item in liswww:
                            dd = self.str_dict(item.split('-'))
                            l.append(dd)
                else:
                    l.append(lis[1].split("-"))
                return l
    
    
    a = AAAA()
    d = a.dd(st, [])
    print(d)
    
  • 相关阅读:
    Network (poj1144)
    C. Hongcow Builds A Nation
    ZYB loves Xor I(hud5269)
    D. Chloe and pleasant prizes
    Game(hdu5218)
    约瑟夫环的递推方法
    Misaki's Kiss again(hdu5175)
    Exploration(hdu5222)
    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses
    C. Arpa's loud Owf and Mehrdad's evil plan
  • 原文地址:https://www.cnblogs.com/mqhpy/p/13365938.html
Copyright © 2011-2022 走看看