회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
적절한 반례가 뭐가 있나요?
20541번 - 앨범정리
kbp0237
3년 전
0
정석적인 트리랑 dfs로 풀이 했습니다.
directory class는 현재 앨범 포인터를 움직이는 역할 합니다
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class folder { private: folder* parent; string name; vector<folder*> folders; vector<string> pictures; public: folder(string name) { parent = this; this->name = name; } folder(folder* parent, string name) { this->parent = parent; this->name = name; } string getName() {return name;} folder* getParent() {return parent;} folder* getFolder(string name) { for(int i = 0; i < this->folders.size(); i++) if (this->folders[i]->getName() == name) return this->folders[i]; return NULL; } void makeFolder(string name); void insertPicture(string name) { this->pictures.push_back(name); sort(this->pictures.begin(), this->pictures.end()); } int pictureCount(folder* curr) { if (curr->folders.size() == 0) { return curr->pictures.size(); } int result = 0; for (int i = 0; i < curr->folders.size(); i++) { result += pictureCount(curr->folders[i]); } result += curr->pictures.size(); return result; } int folderCount(folder* curr) { if (curr->folders.size() == 0) { return 1; } int result = 0; for (int i = 0; i < curr->folders.size(); i++) { result += folderCount(curr->folders[i]); } return result + 1; } void deleteFolder(string command) { if (!this->folders.empty()) { // 현재 앨범의 앨범이 빈 경우가 아니라면 int pictureCnt = 0; int folderCnt = 0; if (command == "-1") { pictureCnt = pictureCount(this->folders[0]); folderCnt = folderCount(this->folders[0]); cout << folderCnt << " " << pictureCnt << "\n"; this->folders.erase(this->folders.begin()); }else if (command == "0") { for (int i = 0; i < this->folders.size(); i++) { pictureCnt += pictureCount(this->folders[i]); folderCnt += folderCount(this->folders[i]); } cout << folderCnt << " " << pictureCnt << "\n"; this->folders.clear(); }else if (command == "1") { pictureCnt = pictureCount(this->folders[this->folders.size() - 1]); folderCnt = folderCount(this->folders[this->folders.size() - 1]); cout << folderCnt << " " << pictureCnt << "\n"; this->folders.erase(this->folders.end() - 1); } }else { cout << 0 << " " << 0 << "\n"; } } void deleteFolderByName(string name) { int pictureCnt = 0; int folderCnt = 0; for (int i = 0; i < this->folders.size(); i++) { if (this->folders[i]->getName() == name) { pictureCnt = pictureCount(this->folders[i]); folderCnt = folderCount(this->folders[i]); cout << folderCnt << " " << pictureCnt << "\n"; this->folders.erase(this->folders.begin() + i); return; } } } void deletePicture(string command) { if (!this->pictures.empty()) { // 현재 앨범의 사진이 빈 경우가 아니라면 if (command == "-1") { cout << 1 << "\n"; this->pictures.erase(this->pictures.begin()); }else if (command == "0") { cout << this->pictures.size() << "\n"; this->pictures.clear(); }else if (command == "-1") { cout << 1 << "\n"; this->pictures.erase(this->pictures.end() - 1); } }else { cout << 0 << "\n"; } } void deletePictureByName(string name) { for(int i = 0; i < this->pictures.size(); i++) { if (this->pictures[i] == name) { this->pictures.erase(this->pictures.begin() + i); cout << 1 << "\n"; return; } } } bool folderExist(string name) { for (int i = 0; i < this->folders.size(); i++) if (this->folders[i]->getName() == name) return true; return false; } bool pictureExist(string name) { for (int i = 0; i < this->pictures.size(); i++) if (this->pictures[i] == name) return true; return false; } friend class directory; }; bool cmp(folder* a, folder* b) { return a->getName() < b->getName(); } void folder::makeFolder(string name) { folder* newFolder = new folder(this, name); this->folders.push_back(newFolder); sort(folders.begin(), folders.end(), cmp); } class directory { private: folder* curr; public: directory(folder* folder) { curr = folder; } folder* getCurr() {return curr;} void setCurr(folder* curr) { this->curr = curr; } bool isRoot() { return curr->getName() == "album"; } void moveParent() { curr = curr->getParent(); cout << curr->getName() << "\n"; } }; int main() { int N; folder* album = new folder("album"); directory* curr = new directory(album); cin >> N; for (int i = 0; i < N; i++) { string command; string command2; cin >> command; if (command == "mkalb") { cin >> command2; if (curr->getCurr()->folderExist(command2)) { // 앨범이 이미 존재하는 경우 cout << "duplicated album name\n"; }else { // 존재하지 않는 경우 curr->getCurr()->makeFolder(command2); } }else if (command == "rmalb") { cin >> command2; if (command2 == "-1") { curr->getCurr()->deleteFolder("-1"); }else if (command2 == "0") { curr->getCurr()->deleteFolder("0"); }else if (command2 == "1") { curr->getCurr()->deleteFolder("1"); }else { if (curr->getCurr()->folderExist(command2)) { // 폴더가 존재한다면 curr->getCurr()->deleteFolderByName(command2); }else { // 존재하지 않으면 cout << 0 << " " << 0 <<"\n"; } } }else if (command == "insert") { cin >> command2; if (curr->getCurr()->pictureExist(command2)) { cout << "duplicated photo name\n"; }else { curr->getCurr()->insertPicture(command2); } }else if (command == "delete") { cin >> command2; if (command2 == "-1") { curr->getCurr()->deletePicture(command2); }else if (command2 == "0") { curr->getCurr()->deletePicture(command2); }else if (command2 == "1") { curr->getCurr()->deletePicture(command2); }else { // 이름으로 지우는 경우 if (curr->getCurr()->pictureExist(command2)) { // 사진이 존재한다면 삭제 curr->getCurr()->deletePictureByName(command2); }else { cout << 0 << "\n"; } } }else if (command == "ca") { cin >> command2; if (command2 == "..") { if (curr->getCurr() != album) { // 현재 앨범이 album아닌 경우 curr->moveParent(); }else { cout << "album\n"; } }else if (command2 == "\/") { curr->setCurr(album); cout << "album\n"; }else { if (curr->getCurr()->folderExist(command2)) { // 앨범이 존재한다면 이동 folder* next = curr->getCurr()->getFolder(command2); curr->setCurr(next); cout << curr->getCurr()->getName() << "\n"; }else { // 존재 안하면 유지 cout << curr->getCurr()->getName() << "\n"; } } } } }
댓글을 작성하려면
로그인
해야 합니다.
kbp0237 3년 전
정석적인 트리랑 dfs로 풀이 했습니다.
directory class는 현재 앨범 포인터를 움직이는 역할 합니다