web123456

"mkdir-p" command

Question link: /questionTerminal/433c0c6a1e604a4795291d9cd7a60c7a

At work, whenever a new machine is deployed, it means there are a bunch of directories to be created. For example, to create the directory "/usr/local/bin", you need to create "/usr", "/usr/local" and "/usr/local/bin". Fortunately, mkdir provides a powerful "-p" option under Linux. Just one command "mkdir -p /usr/local/bin" can automatically create the required upper directory.
Now I will give you some folder directories that need to be created. Please help generate the corresponding "mkdir -p" command

Enter a description:

The input contains multiple sets of data.
The first behavior of each set of data is a positive integer n (1≤n≤1024).
Immediately following n lines, each line contains a directory name to be created. The directory name is composed only of numbers and letters, and the length is no more than 200 characters.

Output description:

For each set of data, output the corresponding "mkdir -p" command sorted in dictionary order.
After each set of data is output a blank line as a separation

Example 1:
enter:

3
/a
/a/b
/a/b/c
3
/usr/local/bin
/usr/bin
/usr/local/share/bin

Output:

mkdir -p /a/b/c

mkdir -p /usr/bin
mkdir -p /usr/local/bin
mkdir -p /usr/local/share/bin

Ideas:Exclude the prefix string path and the same path, and output only the longest or unique path.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//Exclude the prefix string path and the same path, only mkdir -p longest or unique path
int main()
{   
	int n;
	while (cin >> n)
	{
		vector<string> list(n);
		vector<bool> flag(n, true);
		for (int i = 0; i < list.size(); ++i)
		{
			cin >> list[i];
		}
		sort(list.begin(), list.end());//Sort strings
		for (int i = 0; i < list.size()-1; ++i)
		{
			if (list[i] == list[i + 1])//The same two strings
				flag[i] = false;
			else if (list[i].size() < list[i + 1].size() && list[i] == list[i + 1].substr(0, list[i].size())
				&& list[i + 1][list[i].size()] == '/')
				flag[i] = false;//The front string is the substring of the back string and the next bit of the back string is/
		}
		for (int i = 0; i < list.size(); ++i)
			if (flag[i])
				cout << "mkdir -p " <<list[i] << endl;
		cout << endl;
	}
	return 0;
}