【PAT甲级2020年冬季】7-3 File Path (25 分)【 很详细 !!! 】

FP.JPG

The figure shows the tree view of directories in Windows File Explorer. When a file is selected, there is a file path shown in the above navigation bar. Now given a tree view of directories, your job is to print the file path for any selected file.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​3​​), which is the total number of directories and files. Then Nlines follow, each gives the unique 4-digit ID of a file or a directory, starting from the unique root ID 0000. The format is that the files of depth d will have their IDs indented by d spaces. It is guaranteed that there is no conflict in this tree structure.

Then a positive integer K (≤100) is given, followed by K queries of IDs.

Output Specification:

For each queried ID, print in a line the corresponding path from the root to the file in the format: 0000->ID1->ID2->...->ID. If the ID is not in the tree, print Error: ID is not found. instead.

Sample Input:

14
0000
 1234
  2234
   3234
    4234
    4235
    2333
   5234
   6234
    7234
     9999
  0001
   8234
 0002
4 9999 8234 0002 6666

Sample Output:

0000->1234->2234->6234->7234->9999
0000->1234->0001->8234
0000->0002
Error: 6666 is not found.

额,这题我的改正后的代码并没有通过OJ检查,所以不太清楚是否完全通过所有的测试点。

题目给的测试用例应该是可以通过了的:

思路:

其实这道题也很简单,就是按照题意去进行解答就可以。

  1. 定义一个文件夹的结构体,然后定义该结构体数组,循环输入每个文件夹名字,输入后根据名字里的空格数判断它的深度,并把它的id和深度存入数组中。
  2. 对于要验证的每一个文件夹名,先定义一个装路径的 vector<string> ans; 然后遍历(从后向前)整个存目录的结构体数组,看是否存在此目录。若无,打印  Error: XXXX is not found.
  3. 如果可以找到,将其加入ans中,然后从后向前遍历这个目录前的每个目录,遇到的第1个和它的深度差1的就是它的上层目录~ 然后再找上册目录的上层目录,直到找到层数为0的0000号文件夹为止

不难吧,挺容易就能想出来的。

代码:

#include<iostream>
#include<vector>
using namespace std;
struct dir{
	string id; //文件夹名
	int dep;   //文件夹深度
};
int main(){
    int n,k=0;
    cin>>n;
    string s;
    vector<dir> alldir(n);
    getchar();  //* 一定得加上!!!因为后边还有读入一整行的操作!!!
    for(int i=0;i<n;i++){
        getline(cin,s);
		int d=0;//文件夹深度 
        for(int j=0;j<s.size();j++)
        	if(s[j]==' ') d++;  //注意不是:" ",会出error~ 
        	else break;
        alldir.push_back({s.substr(d),d});
    }
	cin>>k;
	for(int i=0;i<k;i++){
		int d,j,flag=0;//当前Id的layer记录层数 
		cin>>s;
		vector<string> ans;//装路径的 
		for(j=alldir.size()-1;j>=0;j--){
			if(alldir[j].id==s){
				d=alldir[j].dep;
				ans.push_back(s); 
				flag=1;
				break;
			}
		}
		if(flag==0) {	//不存在 
			printf("Error: %s is not found.\n",s.c_str());	 
		}else{	//存在 
			int a=j-1;
			while(a>=0){  //从当前目录向前找 别忘了 a--
				if(alldir[a].dep==d-1){
					d--;
					ans.push_back(alldir[a].id); 	
				}
				a--; 
			}
			for(int m=ans.size()-1;m>=0;m--){
				if(m!=ans.size()-1) cout<<"->";
				printf("%s",ans[m].c_str());
			}
			cout<<endl;
		} 	
	}
	return 0;
}

总结:

这道题思路上不难想其实,我用了大约半个小时的时间就写好核心代码了,但是!!!调试了一个半小时,中间出现了各种情况。啊!( 土拨鼠叫 ~

  • 循环变量 i、j 写混的现象频频发生,以后千万要注意 ( 碎碎念~ ~
  • 如果cin或scanf()后有读入一整行的操作例如getline(cin,s),需要在cin、scanf()后加一个getchar(),这个之前碰到过多次,但就是不记,导致了最后一个文件夹名字0002总是无法加入结构数组中。
  • string 字符串的每一个元素是字符char类型,把一个元素转成整数是 s[i]-'0' 而不是#include<cctype>的 stoi(string s) 函数!!! 
  • 对string 字符串的某个元素与一个字符进行比较的时候是是 s[i] == 'c' ,是单引号而不是双引号。
  • 用个flag=0/1做判断比较简洁哦
  • printf("%s", xxx.c_str());   //使用printf打印字符串变量要小心,在变量名后加  .c_str()
  • 。。。。。。注意细节,否则思路对了也因为超时的调试提交不上

猜你喜欢

转载自blog.csdn.net/WKX_5/article/details/114675582