Tab is not replaceable with 4 spaces


  • 0
    K

    I confirm the problem reported in one of the earlier posts. I've got my C++ code confused in the first OJ test case with the spaces: the 4 spaces are different from a single tab and I'm afraid in at least C++ there is no way to handle the two on equal footing. That said, there was a trivial change to my code that accounted for spaces. And now I confused the OJ with this test case: "dir\n\tfile.txt\n\tdir2\n\t\tfile.txt" that should result "dir/dir2/file.txt" to be the longest path of size 17. Try replacing '\t' with four spaces and observe OJ changes its answer to 16! I'm giving up on that problem believing that my clumsy solution is correct. Here it is, btw:

    int lengthLongestPath(string input) {
        int maxLength = 0;
        int curLength = 0;
        int curLevel  = 0;
        list<int> historyLength;
        for(size_t pos=input.find("\n"),prevPos=0,prevLen=0 ; ; prevLen=pos-prevPos,prevPos=pos,pos=input.find("\n",pos+1)){
            int level = 0;
            for(size_t p=prevPos+1; input[p]=='\t' || input[p]==' '; p++) level++;
            while( level < curLevel ){
                curLength = historyLength.back();
                historyLength.pop_back();
                curLevel -= (input[prevPos+1]=='\t' ? 1 : 4);
            }
            if( level > curLevel ){
                historyLength.push_back(curLength);
                curLength += prevLen-curLevel;
                curLevel   = level;
            }
    
            if( pos == string::npos ) pos = input.length();
    
            if( input.substr(prevPos,pos-prevPos).find(".") != string::npos ) // this is file
                if( maxLength < curLength + pos-prevPos-level )
                    maxLength = curLength + pos-prevPos-level;
    
            if( pos == input.length() ) break;
        }
        return maxLength;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.