Problem Statement: Given a file system(consisting of directories, sub-directories, and files) and a target file name, print the absolute path of that targeted file.
Input: Input would be a series of strings. The first line would be the target file name. From the next line onward, there will be multiple directories/sub-directories and file names separated by newlines. And “-” indicates the level hierarchy of each sub-directory or file. Assume that, the targeted file will always be present and will be unique in the input.
Output:
Print the path starting from the root directory name. Put a “/” in between directories, sub-directories, and files while printing the path.
Sample Input:
yes.cpp
Documents
-Homework
–DS.virus
–algo.docx
-yes.cpp
Downloads
-write.cpp
-yes.h
Sample Output:
Documents/yes.cpp
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
string target;
cin>>target;
string str,s,ss, ext;
while(cin>>str)
{
s+=str;
s+="\n";
}
int level=0;
bool flag=0;
deque<string> dq;
for(int i=0; i<s.size(); i++)
{
if(s[i]=='\n')
{
dq.push_back(ss);
if(flag && ss==target)
{
cout<<dq[0];
for(int i=1; i<dq.size(); i++) cout<<"/"<<dq[i];
return 0;
}
ss.clear();
flag=0;
level=0;
}
else if(s[i]=='-') level++;
else if(s[i]=='.')
{
ss+=s[i];
flag=1;
}
else
{
ss+=s[i];
while(level<dq.size()) dq.pop_back();
}
}
return 0;
}