算法入门经典-第六章 例题6-21 SystemDependencies
2024-08-31 09:30:18
- 题意:软件组件之间会有依赖关系,比如你下一个Codeblocks你也得顺带着把编译器给下上。你的任务是模拟安装和卸载软件组件的过程。有以下五种指令,如果指令为“END”则退出程序;若为以下四种指令,则分别作对应操作:
Command Syntax | Interpretation/Response |
DEPEND item1 item2 [item3 ...] | item1 depends on item2 (and item3 ...) |
INSTALL item1 | install item1 and those on which it depends |
REMOVE item1 | remove item1, and those on which it depends, if possible |
LIST |
list the names of all currently-installed components |
在INSTALL指令中提到的组件成为显示安装,这些组件必须用REMOVE指令显示删除。同样,被显示安装的组件直接或间接依赖的其他组件也不能在REMOVE指令中删除。最后就是注意字符串输入与输出的细节问题。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert> using namespace std; const int maxn = ;
int cnt = ;
map<string, int> name2id; // 把名字转化为整数,方便处理
string name[maxn]; vector<int> depend[maxn]; // 组件x所以来的组件列表
vector<int> depended[maxn]; // 依赖于x的组件列表 int status[maxn]; // 0表示组件x未安装,1表示显示安装,2表示隐式安装
vector<int> installed; // 存放安装过的组件,安装过的就不要再安装了 // 把名字转化为整数维护
int ID(const string& item)
{
if (!name2id.count(item)) {
name[++cnt] = item;
name2id[item] = cnt;
}
return name2id[item];
} // 是否有组件依赖于item
bool needed(int item)
{
for (int i = ; i < depended[item].size(); i++) {
if (status[depended[item][i]]) {
return true;
}
}
return false;
} // 安装item,如果有依赖项,递归的继续安装
void install(int item, bool toplevel)
{
if (!status[item]) {
for (int i = ; i < depend[item].size(); i++) {
install(depend[item][i], false);
}
cout << " Installing " << name[item] << endl;
status[item] = toplevel ? : ;
installed.push_back(item);
}
} // 判断是否能删除,如果能,删除之后递归的删除其他所依赖的组件
void remove(int item, bool toplevel)
{
if ((toplevel || status[item] == ) && !needed(item)) {
status[item] = ;
installed.erase(remove(installed.begin(), installed.end(), item), installed.end());
cout << " Removing " << name[item] << endl;
for (int i = ; i < depend[item].size(); i++) {
remove(depend[item][i], false);
}
}
} // 按照安装顺序输出
void list()
{
for (int i = ; i < installed.size(); i++) {
cout << " " << name[installed[i]] << endl;
}
} int main()
{
ios::sync_with_stdio(false);
string line, cmd;
memset(status, , sizeof(status));
while (getline(cin, line)) {
cout << line << endl;
stringstream ss(line);
ss >> cmd;
if (cmd[] == 'E') {
break;
}
string item1, item2;
if (cmd[] == 'L') {
list();
}
else {
ss >> item1;
int i1 = ID(item1);
if (cmd[] == 'D') {
while (ss >> item2) {
int i2 = ID(item2);
depend[i1].push_back(i2);
depended[i2].push_back(i1);
}
}
else if (cmd[] == 'I') {
if (status[i1]) {
cout << " " << item1 << " is already installed.\n";
}
else {
install(i1, true);
}
}
else {
if (!status[i1]) {
cout << " " << item1 << " is not installed.\n";
}
else if (needed(i1)) {
cout << " " << item1 << " is still needed.\n";
}
else {
remove(i1, true);
}
}
}
} return ;
}
最新文章
- 关于MFC OpenGL环境配置的一点总结
- webservice调用服务端数据时给soapenv:Envelope 添加自定义的命名空间
- QTP 10 安装及破解
- IT应届生如何准备找工作?
- Java异常捕获之try-catch-finally-return的执行顺序-转载
- 基于SpringBoot项目的https
- 转:Python时间戳和日期的相互转换
- 爆破vcrkme01(已补上注册机)
- ortp使用详解2
- Wheel ProgressBar 实现之三——模拟进度过程
- UVA 1605 Building for UN
- SQL多个表实现联合查询
- Codeforces Round #189 (Div. 2)
- 每日一帖示例程序(使用TWebBrowser基于HTML做)
- ios应用接入微信开放平台
- 那些学些网址_jquery初学知识
- Maven 设置Maven源/镜像
- 关于使用scrapy框架编写爬虫以及Ajax动态加载问题、反爬问题解决方案
- 微信小程序学习 一
- 细说MySQL数据库操作