package Souhu; import java.util.Scanner;import java.util.Stack; /** Created by gentl on 2017/9/17.*/public class T { public static void main(String[] args){ Scanner in=new Scanner(System.in); String p=in.next(); System.out.println(simplifyPath(p)); } /** @param path the original path @return the simplified path*/ static public String simplifyPath(String path) { Stack<String> stack = new Stack<String>(); String[] str = path.split("/"); //考虑/... for(int i = 0; i < str.length; i++) { //System.out.print(str[i]+" "); if(str[i].equals(".")||str[i].equals("")){ } else if(str[i].equals("..")) { stack.pop(); // System.out.print(ii+" "); } else { stack.add(str[i]); //System.out.println(str[i]+" "); } } if(stack.isEmpty()) return "/"; StringBuilder sb = new StringBuilder(); while(!stack.isEmpty()) { if(stack.peek().equals("")) stack.pop(); else sb.insert(0, "/" + stack.pop()); } return sb.length() == 0 ? "/" : sb.toString();}}
点赞 2

相关推荐

不愿透露姓名的神秘牛友
11-21 19:05
点赞 评论 收藏
分享
牛客网
牛客企业服务