1. 题意:软件组件之间会有依赖关系,比如你下一个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 ;
}

算法入门经典-第六章 例题6-21 SystemDependencies的更多相关文章

  1. 算法入门经典第六章 例题6-14 Abbott的复仇(Abbott's Revenge)BFS算法实现

    Sample Input 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ELF * 2 3 SF ...

  2. 算法入门经典-第七章 例题7-4-1 拓展 n皇后问题 回溯法

    实际上回溯法有暴力破解的意思在里面,解决一个问题,一路走到底,路无法通,返回寻找另   一条路. 回溯法可以解决很多的问题,如:N皇后问题和迷宫问题. 一.概念 回溯算法实际类似枚举的搜索尝试过程,主 ...

  3. 算法入门经典第七章 例题7-2-1 生成1-n的排列

    输入正数n,按字典序从小到大的顺序输出n个数的所有排列.两个序列的字典序大小关系等价于从头开始第一个不相同位置处的大小关系. 递归的边界应该很好理解吧,当集合s[]中没有一个元素的时候,按照上面的伪码 ...

  4. C#入门经典 第六章 委托

    C#入门经典 第六章 6.6 委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字. 委托的声明指定了一个返回类型和一个参数列表. 在定义了委托后,就可以声明该委托类型的变量. 接着 ...

  5. C#入门经典第六章函数-2-委托

    委托:

  6. Oracle编程入门经典 第12章 事务处理和并发控制

    目录 12.1          什么是事务处理... 1 12.2          事务处理控制语句... 1 12.2.1       COMMIT处理... 2 12.2.2       RO ...

  7. Oracle编程入门经典 第11章 过程、函数和程序包

    目录 11.1          优势和利益... 1 11.2          过程... 1 11.2.1       语法... 2 11.2.2       建立或者替换... 2 11.2 ...

  8. Perl语言入门:第六章习题:处理用户所指定的名字并汇报相应的姓。

    37 print "\n----------------------------------_exercise_6_1--------------------------\n";  ...

  9. 算法竞赛入门经典_第二章:循环结构程序设计_上机练习_MyAnswer

    习题2-1 位数 输入一个不超过109的正整数,输出它的位数.例如12735的位数是5.请不要使用任何数学函数,只用四则运算和循环语句实现. #include<stdio.h> int m ...

随机推荐

  1. 如何用PYTHON代码写出音乐

    什么是MIDI 博主本人虽然五音不全,而且唱歌还很难听,但是还是非常喜欢听歌的.我一直在做这样的尝试,就是通过人工智能算法实现机器自动的作词和编曲(在这里预告下,通过深度学习写歌词已经实现了,之后会分 ...

  2. shell学习第二弹-进阶

    1.linux系统中配置文件执行顺序 1)用户登录系统,首页调用/etc/profile文件初始化所有bash用户的默认特征 2)然后shell依次查找~/.bash_profile,~/.bash_ ...

  3. CGContext与上下文

    上下文指的是场景拥有的资源或属性. 上下文的操作包含上下文的设置: 和上下文的引用. 上下文是一个结构体. 主要包含结构体的设置和使用.

  4. day02_20190106 基础数据类型 编码 运算符

    一.格式化输出 name = input('请输入姓名') age = input('请输入年龄') hobby = input('请输入爱好') job = input('请输入你的工作') # m ...

  5. javase 异常处理

    1.简述什么是异常.异常的继承体系?  异常就是java代码块在运行时出现的错误,有编译错误和运行错误,  Throwable是所有异常的父类它包含了error和Exception两个子类.  其中e ...

  6. Elasticsearch学习(一)————简单命令

    Elasticsearch一.简介**Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索.结构化检索和分析,并能将这三者结合起来.Elasticsearch 基于 Lucene ...

  7. bzoj 4372: 烁烁的游戏 动态点分治_树链剖分_线段树

    [Submit][Status][Discuss] Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠. 题意: 给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠. 烁烁他每次会跳 ...

  8. hibernate中session的get和load方法的区别和联系:

    1. get:及时加载,调用到get方法时立即向数据库查询(在没有session缓存的请况). 2. load:默认使用懒加载,当用到数据的时候才向数据库查询(在没有session缓存的请况). 3. ...

  9. .net 导入Excel

    今天我在做导入Excel的时候遇到了一些问题,顺便说句其实我很少做这方面的!我的需求是导入EXCEL 验证数据正确性 并把数据显示到页面 如有错误信息则弹出来 那具体问题是什么呢? 导入Excel有2 ...

  10. freeswich 安装mod_av

    转载   https://www.cnblogs.com/zgen1/p/7867303.html 需要先编译libav库 编译libav下载:git clone https://freeswitch ...