|
| 1 | +package _20160820_1st_contest; |
| 2 | + |
| 3 | +import java.util.Stack; |
| 4 | + |
| 5 | +import utils.CommonUtils; |
| 6 | + |
| 7 | +public class LongestAbsoluteFilePath { |
| 8 | + public static int lengthLongestPath(String input) { |
| 9 | + Stack<Integer> stack = new Stack(); |
| 10 | + int longestLen = 0, currDirLen = 0; |
| 11 | + int i = 0, currLevel = 0; |
| 12 | + int nextLevel = 0; |
| 13 | + boolean isFile = false; |
| 14 | + Character period = '.', space = ' '; |
| 15 | + while(i < input.length()){ |
| 16 | + currLevel = nextLevel; |
| 17 | + int currStrLen = 0; |
| 18 | + while(i < input.length() && |
| 19 | + (Character.isLetterOrDigit(input.charAt(i)) |
| 20 | + || period.equals(input.charAt(i)) || |
| 21 | + space.equals(input.charAt(i)))) { |
| 22 | + if(period.equals(input.charAt(i))) { |
| 23 | + isFile = true; |
| 24 | + } |
| 25 | + i++; currStrLen++; |
| 26 | + } |
| 27 | + if(isFile) { |
| 28 | + longestLen = Math.max(longestLen, currDirLen+currStrLen); |
| 29 | + } |
| 30 | + else { |
| 31 | + currDirLen += currStrLen+1; |
| 32 | + stack.push(currStrLen+1); |
| 33 | + } |
| 34 | + |
| 35 | + nextLevel = 0; |
| 36 | + i = i+1;//increment one to let it pass "\n" and start from "\t" |
| 37 | + while(i < input.length()-1 && input.substring(i, i+1).equals("\t")){ |
| 38 | + nextLevel++; |
| 39 | + i = i+1; |
| 40 | + } |
| 41 | + |
| 42 | + if (nextLevel < currLevel) { |
| 43 | + int j = 0; |
| 44 | + if (isFile) { |
| 45 | + while (!stack.isEmpty() && j < (currLevel - nextLevel)) { |
| 46 | + currDirLen -= stack.pop(); |
| 47 | + j++; |
| 48 | + } |
| 49 | + } else { |
| 50 | + while (!stack.isEmpty() && j <= (currLevel - nextLevel)) { |
| 51 | + currDirLen -= stack.pop(); |
| 52 | + j++; |
| 53 | + } |
| 54 | + } |
| 55 | + } else if(nextLevel == currLevel){ |
| 56 | + if(!isFile && !stack.isEmpty()){ |
| 57 | + currDirLen -= stack.pop(); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + if (nextLevel == 0) { |
| 63 | + currDirLen = 0; |
| 64 | + stack.clear(); |
| 65 | + } |
| 66 | + |
| 67 | + isFile = false; |
| 68 | + } |
| 69 | + |
| 70 | + return longestLen; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + public static void main(String... strings) { |
| 76 | +// System.out.println(Character.isLetterOrDigit('&')); |
| 77 | +// System.out.println(Character.isLetterOrDigit('\\')); |
| 78 | +// System.out.println(Character.isValidCodePoint('#')); |
| 79 | + String test = "\\t"; |
| 80 | +// System.out.println(test.substring(0, 2).equals("\\t")); |
| 81 | +// System.out.print("\n\t"); |
| 82 | +// System.out.print("something"); |
| 83 | +// String s = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext";//correct output should be 32 |
| 84 | +// String s = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";//correct output is 20 |
| 85 | +// String s = "aaaaaaaaaaaaaaaaaaaaa/sth.png"; |
| 86 | +// String s = "a/aa/aaa/file1.txt"; |
| 87 | +// String s = "file name with space.txt"; |
| 88 | +// String s = "dir\n file.txt"; |
| 89 | + String s ="dir\n file.txt";//correct output is 12 |
| 90 | +// String s = "a\n\tb1\n\t\tf1.txt\n\taaaaa\n\t\tf2.txt";//correct answer is 14 |
| 91 | + printWithIndex(s); |
| 92 | + System.out.println(s); |
| 93 | + System.out.println(lengthLongestPath(s)); |
| 94 | +// System.out.println(parse(s)); |
| 95 | + } |
| 96 | + |
| 97 | + private static void printWithIndex(String s) { |
| 98 | + System.out.println("\\n"); |
| 99 | + int len = s.length(); |
| 100 | + for(int i = 0; i < len; i++){ |
| 101 | + System.out.print(i); |
| 102 | + System.out.print("\t"); |
| 103 | + } |
| 104 | + System.out.println(); |
| 105 | + Character slash = '\\', space = ' ', n = 'n', t = 't'; |
| 106 | + String newLine = "\\n", newTab = "\\t"; |
| 107 | + for(int i = 0; i < len; i++){ |
| 108 | + switch (s.charAt(i)){ |
| 109 | + case '\n': System.out.print("\\" + " " + "n"); break; |
| 110 | + case '\t': System.out.print("\\" + " " + "t"); break; |
| 111 | + default: System.out.print(s.charAt(i)); |
| 112 | + } |
| 113 | + System.out.print("\t"); |
| 114 | + } |
| 115 | + System.out.println(); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + public static int parse(String input){ |
| 121 | + String[] splits = input.split("\\n"); |
| 122 | + CommonUtils.printArray_generic_type(splits); |
| 123 | + int longestLen = 0; |
| 124 | + for(String path : splits){ |
| 125 | + boolean isFile = false; |
| 126 | + int thisLen = 0; |
| 127 | + String[] paths = path.split("\\t"); |
| 128 | + CommonUtils.printArray_generic_type(paths); |
| 129 | + if(paths[paths.length-1].contains(".")) isFile = true; |
| 130 | + if(isFile){ |
| 131 | + for(String eachDir : paths){ |
| 132 | + thisLen += eachDir.length(); |
| 133 | + thisLen++;//plus the slash sign |
| 134 | + } |
| 135 | + longestLen = Math.max(longestLen, thisLen); |
| 136 | + } |
| 137 | + } |
| 138 | + return longestLen; |
| 139 | + } |
| 140 | +} |
0 commit comments