Uva - 506 - System Dependencies
模拟题,注意显示安装和隐式安装,显示安装的必须显示显示删除。把名字转化为整数维护。其他注意都注释了。输入稍微多一下,题目不是很麻烦。
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的更多相关文章
- 【STL+模拟】UVa 506 - System Dependencies
System Dependencies Components of computer systems often have dependencies--other components that m ...
- UVa 506 System Dependencies (细节问题)
题意:输入几种指令,让你进行模拟操作,指令如下: DEPEND item1 item2 (item3 ...) 安装item1需要先安装item2(.item3……) INSTALL item1 安装 ...
- UVA 506 System Dependencies(模拟 烂题)
https://vjudge.net/problem/UVA-506 题目是给出了五种指令,DEPEND.INSTALL.REMOVE.LIST.END,操作的格式及功能如下: DEPEND item ...
- Learning ROS: Managing System dependencies
Download and install the system dependencies for turtlesim: roscd turtlesim cat package.xml rosdep i ...
- 【例题 6-21 UVA - 506】System Dependencies
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个物品它的依赖有哪些,以及它被哪些东西依赖就可以了. 显式安装的东西不能被隐式删除删掉(就是remove item,然后删除i ...
- C C++ TDD单元测试非常好的书
http://product.china-pub.com/199003 测试驱动的嵌入式C语言开发 Test Driven Development for Embedded C <测试驱动的嵌入 ...
- Debian8.3.0下安装Odoo8.0步骤
Debian8.3.0下安装Odoo8.0的方法 假设你已经安装好了Debian 系统,使用root帐号执行如下命令 # apt-get update && apt-get upgra ...
- Classes
Class Organization Following the standard Java convention, a class should begin with a list of varia ...
- maven-dependency-plugin插件的使用
maven-dependency-plugin插件的使用 maven-dependency-plugin是 处理与依赖相关的插件.它有很多可用的goal,大部分是和依赖构建.分析和解决相关的goa ...
随机推荐
- 吴恩达深度学习第2课第2周编程作业 的坑(Optimization Methods)
我python2.7, 做吴恩达深度学习第2课第2周编程作业 Optimization Methods 时有2个坑: 第一坑 需将辅助文件 opt_utils.py 的 nitialize_param ...
- Js删除数组函数
引用地址:http://www.398588.com/Article/T8/96.aspx 众所周知大家都应该知道delete的用法,用来删除对象或者删除数组等,如果是删除对象除数组以外可能可以删除的 ...
- php序列化漏洞理解
0x01什么是序列化 序列化就是将我们的 对象转变成一个字符串,保存对象的值方便之后的传递与使用. 0x02为什么要序列化 如果为一个脚本中想要调用之前一个脚本的变量,但是前一个脚本已经执行完毕,所有 ...
- 使用redis做缓存
redis常本用来作为缓存服务器.缓存的好处是减少服务器的压力,数据查询速度快.解决数据响应慢的问题. 添加缓存:只用redis的Hash数据类型添加缓存. 例如:需要在查询的业务功能中,添加缓存 1 ...
- python中的printf:%号拼接字符串和format函数
在C语言中,我们使用printf("%s","hello")这种形式进行字符串的拼接 在python中,进行这样的拼接有两种实现方式,分别是%号拼接以及使用fo ...
- ACM Bee
In Africa there is a very special species of bee. Every year, the female bees of such species give b ...
- SpringMVC总结(SSM)
Day1 1. springMvc:是一个表现层框架: 作用:就是从请求中接收传入的参数, 将处理后的结果数据返回给页面展示2. ssm整合: 1)Dao层 pojo和映射文件以及接口手动编写(或使用 ...
- Activity的四种启动模式任务栈图解
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 今天带来另一篇Activity的文章--Activity的四种启动模式.该篇文章,会以图文讲解的方式带你彻底掌握Activity的启动 ...
- Android开发之手把手教你写ButterKnife框架(二)
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52664112 本文出自:[余志强的博客] 上一篇博客Android开 ...
- TV Metro界面(仿泰捷视频TV版)源码解析
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52822499 前言:上一篇介绍了 ...