public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int p = in.nextInt();
String a = in.nextLine();//读取多余换行符
String str = in.nextLine();
String []s = new String[p];
for(int i = 0; i < p; i++ ) {
s[i] = in.nextLine();
}
for(int i = 0; i < p; i++ ) {
if(s[i].length() == 0) {//空字符默认为相对路径 但是不能直接输出 因为 可能 输入的当前路径str 也是待正则化的 路径
s[i] = str+s[i];
}
}
for(int i = 0; i < p; i++ ) {
if(s[i].charAt(0) != '/') {
s[i] = str + "/"+s[i];//路径为相对路径时
}
}
int i, pos;
for(i = 0; i < p; i++ ) {
//处理//
while((pos = s[i].indexOf("//")) != -1) {
int count = 2;
while(pos + count != s[i].length() && s[i].charAt(pos+count) == '/') {
count++;
}
s[i] = s[i].substring(0, pos) + s[i].substring(pos+count-1, s[i].length());
}
//处理/./
while((pos = s[i].indexOf("/./"))!= -1) {
s[i] = s[i].substring(0, pos+1) + s[i].substring(pos+3,s[i].length());
}
//处理/../
while((pos = s[i].indexOf("/../"))!= -1) {
if(pos == 0) {
s[i] = s[i].substring(3, s[i].length());
}
else {
int tPos1 = s[i].lastIndexOf("/", pos-1);
s[i] = s[i].substring(0, tPos1) + s[i].substring(pos+3, s[i].length());
}
}
//处理末尾/
if(s[i].length() > 1 && s[i].charAt(s[i].length()-1) == '/')
s[i] = s[i].substring(0, s[i].length()-1);
System.out.println(s[i]);
}
}
}
csp 2016_4_3 路径解析
最新推荐文章于 2023-03-13 17:07:23 发布