Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit857b48c

Browse files
longest absolute file path
1 parentcaea4a4 commit857b48c

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package_20160820_1st_contest;
2+
3+
importjava.util.Stack;
4+
5+
importutils.CommonUtils;
6+
7+
publicclassLongestAbsoluteFilePath {
8+
publicstaticintlengthLongestPath(Stringinput) {
9+
Stack<Integer>stack =newStack();
10+
intlongestLen =0,currDirLen =0;
11+
inti =0,currLevel =0;
12+
intnextLevel =0;
13+
booleanisFile =false;
14+
Characterperiod ='.',space =' ';
15+
while(i <input.length()){
16+
currLevel =nextLevel;
17+
intcurrStrLen =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+
intj =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+
}elseif(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+
returnlongestLen;
71+
}
72+
73+
74+
75+
publicstaticvoidmain(String...strings) {
76+
// System.out.println(Character.isLetterOrDigit('&'));
77+
// System.out.println(Character.isLetterOrDigit('\\'));
78+
// System.out.println(Character.isValidCodePoint('#'));
79+
Stringtest ="\\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+
Strings ="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+
privatestaticvoidprintWithIndex(Strings) {
98+
System.out.println("\\n");
99+
intlen =s.length();
100+
for(inti =0;i <len;i++){
101+
System.out.print(i);
102+
System.out.print("\t");
103+
}
104+
System.out.println();
105+
Characterslash ='\\',space =' ',n ='n',t ='t';
106+
StringnewLine ="\\n",newTab ="\\t";
107+
for(inti =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+
publicstaticintparse(Stringinput){
121+
String[]splits =input.split("\\n");
122+
CommonUtils.printArray_generic_type(splits);
123+
intlongestLen =0;
124+
for(Stringpath :splits){
125+
booleanisFile =false;
126+
intthisLen =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(StringeachDir :paths){
132+
thisLen +=eachDir.length();
133+
thisLen++;//plus the slash sign
134+
}
135+
longestLen =Math.max(longestLen,thisLen);
136+
}
137+
}
138+
returnlongestLen;
139+
}
140+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp