SRM405 Div1Easy RelativePath
解法
先にstringの階層構造をvector
class RelativePath { public: vector<string> make(string str) { int N = str.size(); for(int i=0; i<N; i++) { if(str[i] == '/') { str[i] = ' '; } } stringstream ss(str); vector<string> ret; ret.push_back("ROOT"); while(ss >> str) ret.push_back(str); return ret; } string makeRelative(string path, string currentDir) { vector<string> Path = make(path); vector<string> CDir = make(currentDir); int same = 0; for(int i=0; i<min(Path.size(), CDir.size()); i++, same ++) { if(Path[i]!=CDir[i]) { break; } } string ret; while(same < CDir.size()) { CDir.pop_back(); ret += "../"; } for(int i=same; i<Path.size(); i++) { ret += Path[i] + "/"; } ret.pop_back(); return ret; } };