Topcoder Div 2算法

时间:2015-10-03 06:51:02

标签: c++ algorithm binary-search-tree

您好我正在尝试解决此处给出的编码器问题IncompleteBST: http://community.topcoder.com/stat?c=problem_statement&pm=6713&rd=9999

我的方法是: 首先验证输入是否为BST

1. get the number associated with the current node

2. keep dividing it by 2 until the quotient appears in the input list
   this quotient is the parent/ancestor of the current node
3. check if current node is left/right child of the ancestor
   isValidChild() function does this.
   i) It converts both of the numbers to binary representation
  ii) 0 in the binary rep indicates left child and 1 indicates right             
      child
  iii) At the point of difference in the binary rep check if the next      
       el is 0 (left) or right(1)
   iv) If left the associated character should be <= cur,else > cur
4) If it is a BST getTheValues()

代码如下:

    class IncompleteBST {

public:

    vector<int> binaryRep(unsigned long long num) {

        cout<<"\nBinart Rep start\n";

        vector<int> vBinary;
        while(num > 0) {
            vBinary.insert(vBinary.begin(),num%2);
            num/=2;
        }
        //vBinary.insert(vBinary.begin(),num);
        cout<<"\nBinary rep end\n";
        return vBinary;
    }


    bool isValidChild(unordered_map<unsigned long long ,char>& map, unordered_map<unsigned long long,char>::iterator childItr,unordered_map<unsigned long long,char>::iterator parentItr) {

        cout<<"\nIsValidChild Start\n";

        if(childItr == map.end())
          cout<<"\nChild itr is null\n";

        if(parentItr == map.end())
            cout <<"\nparent itr is null\n";

        vector<int> vChild = binaryRep(childItr->first);
        vector<int> vParent = binaryRep(parentItr->first);


        int i;
        for(i =0;i<vParent.size();i++) {
            if(vChild[i] != vParent[i])
                break;
        }

        if(vChild[i] == 1 && childItr->second > parentItr->second) {
            cout <<"\nisValidChild if end \n";
            return true;
        }
        else if(vChild[i] == 0 && childItr->second < parentItr->second) {
            cout <<"\nisValidChild else if end\n";
            return true;
        }
        else {
            cout<<"\nisValidChild else end\n";
            return false;
        }

    }

    bool isBST(unordered_map<unsigned long long,char>& map) {

       cout<<"\nisBst start";

        unordered_map<unsigned long long,char>::iterator it;
        for(it = map.begin(); it != map.end(); it++) {
          if(it->first != 1 && it->second != '?') {
            unsigned long long num = it->first;
            unordered_map<unsigned long long,char>::iterator resultItr;
            cout<<"\nFInding parent itr for"<<num<<endl;
            while(num > 1) {
                num /= 2;
                resultItr = map.find(num);

                if(resultItr != map.end()) {
                   if(resultItr->second == '?')
                    continue;
                   else
                    break;
                }
            }

            if (it->second != '?' && resultItr->second != '?' && !isValidChild(map,it,resultItr)) {
                cout<<"\nIsBST end if\n";
                return false;
            }
         }

        }

        cout<< "\nIsBST end\n";

        return true;
    }

    string getValues(unordered_map<unsigned long long,char>& map,unsigned long long missingNum) { 

       cout<<"\ngetValueSStart\n";

        unsigned long long tmp = missingNum;
        unordered_map<unsigned long long,char>::iterator it;
        while(tmp>1) {
            tmp/=2;
            it = map.find(tmp);
            if(it != map.end())
                break;
        }

        vector<int> vChild = binaryRep(missingNum);
        vector<int> vParent = binaryRep(it->first);

        int i;
        for( i =0;i<vParent.size();i++) {
            if(vChild[i] != vParent[i])
                break;
        }

        string strResult="";
        if(vChild[i] == 1) {
            for(char ch = it->second+1;ch<='Z';ch++)
                strResult += ch;
        }else {
            for(char ch = 'A'; ch<=it->second;ch++)
                strResult += ch;
        }
        cout<<"\ngetValuesEnd\n";
        return strResult;


    }

    string missingValues(vector<string> tree) {

        cout<< "\n Missing Values START \n";
        unordered_map<unsigned long long,char> map;

        int sz = tree.size();

        unsigned long long missingNum = 0;
        for(int i=0; i<sz; i++) {

            vector<string> vTokens;
            split(tree[i],' ',vTokens);
            unsigned long long num = stoull(vTokens[1]);
            map.insert(make_pair(num,vTokens[0][0]));
            if(vTokens[0] == "?") 
                missingNum = num;
        }

        if( !isBST(map)) {
            return "";
        }

        return getValues(map,missingNum);

    }

    void split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }

}


};

这种方法给了我错误的答案。你能帮我解决一下吗

0 个答案:

没有答案