zoukankan      html  css  js  c++  java
  • 第08讲树

    课件(下载

    【POJ2255】Tree Recovery

    #include <iostream>
    #include
    <string>
    using namespace std;

    #define N 27

    char pre[N];
    char in[N];

    void PostOrder(int preStart,int preEnd,int inStart,int inEnd)
    {
    if( preStart <= preEnd )
    {
    char root = pre[preStart];

    int leftLen;
    for( int i=inStart;i<=inEnd;i++ )
    {
    if( root == in[i] )
    {
    leftLen
    = i - inStart;
    break;
    }
    }

    PostOrder(
    preStart
    +1,
    preStart
    + leftLen,
    inStart,
    inStart
    +leftLen-1
    );
    PostOrder(
    preStart
    + leftLen+1,
    preEnd,
    inStart
    + leftLen + 1,
    inEnd
    );

    cout
    <<root;
    }
    }

    int main()
    {
    while(cin>>pre>>in)
    {
    PostOrder(
    0,strlen(pre)-1,0,strlen(in)-1);
    cout
    <<endl;
    }
    return 1;
    }

    【ZOJ1167】Trees on the Level

    #include <map>
    #include
    <string>
    #include
    <iostream>
    #include
    <stdlib.h>
    using namespace std;

    class MySort
    {
    public:
    bool operator ()(const string &_A,const string &_B) const
    {
    if(_A.length() < _B.length() )
    return true;
    if(_A.length()==_B.length() && _A < _B )
    return true;
    return false;
    }
    };

    map
    <string,int,MySort> data;

    bool InsertNode(string str)
    {
    str
    = str.substr(1,str.length()-2);
    int pos = str.find(',');
    string key = str.substr(pos+1);
    int value = atoi( str.substr(0,pos).c_str() );

    if( data.find(key)!=data.end() )
    return false;
    else
    {
    pair
    <map<string,int,MySort>::iterator,bool> insertPair;
    insertPair
    = data.insert( map<string,int,MySort>::value_type(key,value) );
    return insertPair.second;
    //data[key] = value;
    //return true;
    }
    }

    bool HasRoot()
    {
    //cout<<"asdf';
    for(map<string,int,MySort>::iterator i=data.begin();i!=data.end();i++)
    {
    string str = i->first;

    //cout << ' ' << str.substr(0,str.length()-1) << ' ';

    if( str.length() == 0 )
    continue;
    if( data.find(str.substr(0,str.length()-1)) == data.end() )
    return false;
    }
    return true;
    }

    int main()
    {
    string str;
    bool complete = true;
    while( cin>>str )
    {
    if(str == "()")
    {
    //一个测试结束,给出结论
    if( complete && HasRoot() )
    {
    map
    <string,int,MySort>::iterator i;
    for( i=data.begin();i!=data.end();i++ )
    {
    if( i!=data.begin() )
    cout
    << ' ';
    cout
    << (*i).second ;

    }
    }
    else
    {
    cout
    << "not complete";
    }

    cout
    << endl;

    //新测试开始
    //初始化
    data.clear();
    complete
    = true;
    }
    else
    {
    //加入map
    if( !InsertNode(str) )
    complete
    = false;
    }
    }
    return 1;
    }
  • 相关阅读:
    C#第十六节课
    Hadoop系列(三):hadoop基本测试
    Hadoop系列(二):Hadoop单节点部署
    Hadoop系列(一):Hadoop集群搭建
    Stars project
    Tornado实现多进程/多线程的HTTP服务
    python paramiko模块
    爬虫代理
    tornado之用户验证装饰器
    tornado自定义session
  • 原文地址:https://www.cnblogs.com/tuty/p/1860478.html
Copyright © 2011-2022 走看看