Why OJ gave result different from my computer for this solution


  • 0
    G

    The code below gave correct result on my computer, but weird result on OJ.

    Input:"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
    Output on my computer: 32
    Output on OJ: 32680.

    I am using Windows 10 and Visual Studio Express 2015 Version 14.0.24720.00.

    Can anybody tell me the reason?

    class Solution {
    public:
        int lengthLongestPath(string input) {
            int n = input.size();
        	// maxLenTab(k) = v iff max length of strings with k tabs is v
        	map<int, int> maxLenTab;
        	for(int i=0; i<n; i++){
        	    maxLenTab[i] = 0;
        	}
        	// numTabs[i] = v iff number of tabs till character i == v
        	vector<int> numTabs(n, 0);
        	// maxLen[i] = v iff max length of absolute path at character i == v
        	vector<int> maxLen(n, 0);
        	// is character in a path or file name 
        	bool isInFileName = false;
        	for (int i = 0; i<n; i++){
        		if (input[i] == '\n' ){
        			isInFileName = false;
        			numTabs[i] = 0;
        			maxLen[i] = -1;
        			if(i>0){
        				int curLenTab = maxLen[i - 1];
        				maxLenTab[numTabs[i-1]] = max(maxLenTab[numTabs[i - 1]], curLenTab);
        			}
        		}
        		else if (input[i] == '\t'){
        			numTabs[i] = (i==0 ? 1 : numTabs[i - 1] + 1);
        			maxLen[i] = -1;
        		}
        		else{
        			numTabs[i] = (i == 0 ? 0 : numTabs[i - 1]);
        
        			if (!isInFileName){
        				isInFileName = true;
        				maxLen[i] = numTabs[i]==0 ? 1 : maxLenTab[numTabs[i]-1] + 1;
        			}
        			else{
        				maxLen[i] = (i==0? 1 : maxLen[i - 1] + 1);
        			}
        		}
        	}
        
        	int res;
        	int i = 0;
        	while(i < n){
        	    // find path of a file name
        	    bool isFile = false;
        	    int segmentLength = 0;
        	    while( i < n && input[i] != '\t' && input[i] != '\n' ){
        	        if( input[i] == '.' ){
        	            isFile = true;
        	        }
        	        segmentLength = maxLen[i] + numTabs[i]; 
        	        i++;
        	    }
        	    
        	    if( isFile ){
        	        // update result using path of file name
        		    res = max(res, segmentLength );
        	    }
        	    
        	    i++;
        	}
        
        	return res;
        }
    };
    

  • 0
    J

    Sometimes it happens. Different IDE might have different parameter even they all use the same version g++ to compile. The most accurate way to check is to use a g++ to compile yourself and see whether the .o file gives you the correct answer. Good luck.


Log in to reply
 

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