模拟题,注意显示安装和隐式安装,显示安装的必须显示显示删除。把名字转化为整数维护。其他注意都注释了。输入稍微多一下,题目不是很麻烦。

AC代码:

#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 = 10000;
int cnt = 0;
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 = 0; 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 = 0; i < depend[item].size(); i++) {
			install(depend[item][i], false);
		}
		cout << "   Installing " << name[item] << endl;
		status[item] = toplevel ? 1 : 2;
		installed.push_back(item);
	}
}

// 判断是否能删除,如果能,删除之后递归的删除其他所依赖的组件
void remove(int item, bool toplevel)
{
	if ((toplevel || status[item] == 2) && !needed(item)) {
		status[item] = 0;
		installed.erase(remove(installed.begin(), installed.end(), item), installed.end());
		cout << "   Removing " << name[item] << endl;
		for (int i = 0; i < depend[item].size(); i++) {
			remove(depend[item][i], false);
		}
	}
}

// 按照安装顺序输出
void list()
{
	for (int i = 0; i < installed.size(); i++) {
		cout << "   " << name[installed[i]] << endl;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	string line, cmd;
	memset(status, 0, sizeof(status));
	while (getline(cin, line)) {
		cout << line << endl;
		stringstream ss(line);
		ss >> cmd;
		if (cmd[0] == 'E') {
			break;
		}
		string item1, item2;
		if (cmd[0] == 'L') {
			list();
		}
		else {
			ss >> item1;
			int i1 = ID(item1);
			if (cmd[0] == 'D') {
				while (ss >> item2) {
					int i2 = ID(item2);
					depend[i1].push_back(i2);
					depended[i2].push_back(i1);
				}
			}
			else if (cmd[0] == '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 0;
}

Uva - 506 - System Dependencies的更多相关文章

  1. 【STL+模拟】UVa 506 - System Dependencies

    System Dependencies  Components of computer systems often have dependencies--other components that m ...

  2. UVa 506 System Dependencies (细节问题)

    题意:输入几种指令,让你进行模拟操作,指令如下: DEPEND item1 item2 (item3 ...) 安装item1需要先安装item2(.item3……) INSTALL item1 安装 ...

  3. UVA 506 System Dependencies(模拟 烂题)

    https://vjudge.net/problem/UVA-506 题目是给出了五种指令,DEPEND.INSTALL.REMOVE.LIST.END,操作的格式及功能如下: DEPEND item ...

  4. Learning ROS: Managing System dependencies

    Download and install the system dependencies for turtlesim: roscd turtlesim cat package.xml rosdep i ...

  5. 【例题 6-21 UVA - 506】System Dependencies

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个物品它的依赖有哪些,以及它被哪些东西依赖就可以了. 显式安装的东西不能被隐式删除删掉(就是remove item,然后删除i ...

  6. C C++ TDD单元测试非常好的书

    http://product.china-pub.com/199003 测试驱动的嵌入式C语言开发 Test Driven Development for Embedded C <测试驱动的嵌入 ...

  7. Debian8.3.0下安装Odoo8.0步骤

    Debian8.3.0下安装Odoo8.0的方法 假设你已经安装好了Debian 系统,使用root帐号执行如下命令 # apt-get update && apt-get upgra ...

  8. Classes

    Class Organization Following the standard Java convention, a class should begin with a list of varia ...

  9. maven-dependency-plugin插件的使用

    maven-dependency-plugin插件的使用   maven-dependency-plugin是 处理与依赖相关的插件.它有很多可用的goal,大部分是和依赖构建.分析和解决相关的goa ...

随机推荐

  1. Lintcode391 Number of Airplanes in the Sky solution 题解

    [题目描述] Given an interval list which are flying and landing time of the flight. How many airplanes ar ...

  2. matlab sparse函数和full函数用法详解(转)

    sparse函数 功能:Create sparse matrix-创建稀疏矩阵 用法1:S=sparse(X)--将矩阵X转化为稀疏矩阵的形式,即矩阵X中任何零元素去除,非零元素及其下标(索引)组成矩 ...

  3. MongoDB 原子操作

    mongodb不支持事务,所以,在你的项目中应用时,要注意这点.无论什么设计,都不要要求mongodb保证数据的完整性. 但是mongodb提供了许多原子操作,比如文档的保存,修改,删除等,都是原子操 ...

  4. Docker Kubernetes 项目

    Kubernetes 是 Google 团队发起并维护的基于Docker的开源容器集群管理系统,它不仅支持常见的云平台,而且支持内部数据中心. 建于Docker之上的Kubernetes可以构建一个容 ...

  5. 网络七层OSI模型简介

    0.  网络七层OSI模型(Open System Interconnection)总览: 1.  应用层 2.  表示层 :表示层的作用是使通信的应用程序能够解释交换数据的含义.这些服务包括数据压缩 ...

  6. Dynamics 365 Web Api之基于single-valued navigation property的filter查询

    本篇要讲的是dynamics 新版本中web api的一个改进功能,虽然改进的很有限,但至少是改进了. 举个例子,我们现在知道联系人的名字vic,我们想找出客户记录中主要联系人名字为vic的所有客户, ...

  7. 谈一谈CloudBlog的系统架构

    ---------------------------------------------------------------------------------------------[版权申明:本 ...

  8. JBOSS EAP实战(2)-集群、NGINX集成、队列与安全

    JBOSS HTTP的Thread Group概念 JBOSS是一个企业级的J2EE APP Container,因此它和任何一种成熟的企业级中间件一样具有Thread Group的概念.所谓Thre ...

  9. 20160210.CCPP体系详解(0020天)

    程序片段(01):01.二级指针.c 内容概要:二级指针 #include <stdio.h> #include <stdlib.h> //01.二级指针: // 1.使用场景 ...

  10. Android简易实战教程--第四十三话《上拉加载与下拉刷新》

    ListView的下拉刷新很常见,很多开源的框架都能做到这个效果,当然也可以自己去实现.本篇案例是基于xlistview的. 布局: <RelativeLayout xmlns:android= ...