Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

大废题。

Posted by KatrineYang at 2016-07-10 17:17:14 on Problem 1057
#include <iostream>
#include <string>
#include <vector>
#include <stack>

using namespace std;

int partion(vector<string>& array, int p, int r) {
		string x = array[r];
		int i = p - 1;
		int j;
		for (j = p; j < r; j++) {
			if (array[j] < x) {
				i++;
				string temp = array[j];
				array[j] = array[i];
				array[i] = temp;
			}
		}
		string temp = array[j];
		array[j] = array[i + 1];
		array[i + 1] = temp;
		return i+1;
}

void quickSort(vector<string>& array, int p, int r) {
		if (p < r) {
			int q = partion(array, p, r);
			quickSort(array, p, q - 1);
			quickSort(array, q + 1, r);
		}
}

void quickSort(vector<string>& array){
	if(array.empty()) return;
	quickSort(array, 0, array.size()-1);
}

bool isFile(string s){
	return s[0] == 'f';
}

bool isDir(string s){
	return s == "ROOT" || s[0] == 'd';
}

class dir{
public:
	string name;
	vector<string> files;
	dir(string n){
		name = n;
	}
	dir(){}
};

int main() {
	string indent = "|     ";
	int cases = 0;
	while(1){
		cases ++;
		stack<dir> dirs;
		dir root("ROOT");
		dirs.push(root);
		//cout << "ROOT" << endl;
		string s;
		bool printed = false;
		while(cin >> s){
			if(s == "#") return 0;
			//if(s == "*") break;
			if(!printed){
				cout << "DATA SET " << cases << ":" << endl;
				cout << "ROOT" << endl;
				printed = true;
			}
			if(isDir(s)){
				dir newDir(s);
				int depth = dirs.size();
				for(int i = 0; i < depth; i++) cout << indent;
				cout << newDir.name << endl;
				dirs.push(newDir);
			}
			if(isFile(s)){
				dirs.top().files.push_back(s);
			}
			if(s == "]" || s == "*"){
				quickSort(dirs.top().files);
				int filegs = dirs.top().files.size();
				int depth = dirs.size() - 1;
				for(int i = 0; i < filegs; i++){
					for(int j = 0; j < depth; j++) cout << indent;
					cout << dirs.top().files[i] << endl;
				}
				dirs.pop();
			}
			if(s == "*") break;
		}
		cout << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator