https://vjudge.net/problem/UVA-506

  题目是给出了五种指令,DEPEND、INSTALL、REMOVE、LIST、END,操作的格式及功能如下:

DEPEND item1 item2 (item3 ...) 安装item1需要先安装item2(、item3……)
INSTALL item1 安装item1,如果item1依赖其他组件,则先安装其依赖的其他组件
REMOVE item1 移除item1及其依赖的全部组件,如果组件被其他程序依赖,则不移除
LIST 输出当前已安装的所有组件
END 结束程序

  INSTALL指令中作为参数的组件的安装是显式安装,其所依赖的组件的安装不是显示安装。被显式安装的组件只能通过REMOVE指令显式删除,而移除中的组件如果当前被其他显式安装的组件直接或间接地依赖,则不能通过REMOVE移除。

  每当输入指令之后,先原样输出一遍指令,然后执行指令。安装组件时如果可以安装组件则需输出“Installing
组建名”。如果显式安装时组件已被安装则提示“组建名 is already
installed.”,隐式安装则无需提示。如果显式删除组件时组件未被安装则提示“组建名 is not
installed.”,如果组件被其他组件依赖则提示“组建名 is still needed.”,隐式卸载则无需提示。

  其实就是个用STL做的复杂模拟,纯属吃力不讨好的题目,并学不到什么算法思想,唯一的亮点就是为一个组件建立依赖和被依赖两个列表,其他的操作模拟即可。

 1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 #include <sstream>
5 #include <set>
6 #include <vector>
7 #include <stack>
8 #include <map>
9 #include <queue>
10 #include <deque>
11 #include <cstdlib>
12 #include <cstdio>
13 #include <cstring>
14 #include <cmath>
15 #include <ctime>
16 #include <functional>
17 using namespace std;
18
19 int cnt = 0, status[maxn];
20 vector<int> depend[maxn], depend2[maxn];
21 vector<int> installed;
22 string name[maxn];
23
24 int get_id(const string & x)
25 {
26 for (int i = 0; i < cnt; i++)
27 if (name[i] == x)
28 return i;
29 name[cnt++] = x;
30 return cnt-1;
31 }
32
33 void instal(int item, bool top)
34 {
35 if (!status[item]){
36 for (size_t i = 0; i < depend[item].size(); i++)
37 instal(depend[item][i], false);
38 cout << " Installing " << name[item] << endl;
39 status[item] = top ? 1 : 2;
40 installed.push_back(item);
41 }
42 else if(top)
43 cout << " " << name[item] << " is already installed." << endl;
44 }
45
46 bool needed(int item)
47 {
48 for (size_t i = 0; i < depend2[item].size(); i++)
49 if (status[depend2[item][i]])
50 return true;
51 return false;
52 }
53
54 void Remove(int item, bool top)
55 {
56 if(top && status[item]==0)
57 cout << " " << name[item] << " is not installed." << endl;
58 else if(top && needed(item))
59 cout << " " << name[item] << " is still needed." << endl;
60 else if ((top || status[item] == 2) && !needed(item)){
61 status[item] = 0;
62 installed.erase(remove(installed.begin(), installed.end(), item), installed.end());
63 cout << " Removing " << name[item] << endl;
64 for (size_t i = 0; i < depend[item].size(); i++)
65 Remove(depend[item][i], false);
66 }
67 }
68
69 int main()
70 {
72 ios::sync_with_stdio(false);
73 string line;
74 while (getline(cin, line), line != "END")
75 {
76 cout << line << endl;
77 stringstream ss(line);
78 if (line[0] == 'L'){
79 for (size_t i = 0; i != installed.size(); ++i)
80 cout << " " << name[installed[i]] << endl;
81 }
82 else{
83 string t1, t2, t3;
84 ss >> t1 >> t2;
85 if (t1[0] == 'D'){
86 while (ss >> t3){
87 depend[get_id(t2)].push_back(get_id(t3));
88 depend2[get_id(t3)].push_back(get_id(t2));
89 }
90 }
91 else if (t1[0] == 'I')
92 instal(get_id(t2), true);
93 else if (t1[0] == 'R')
94 Remove(get_id(t2), true);
95 }
96 }
97 cout << "END" << endl;
98 return 0;
99 }

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

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

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

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

  4. uva 10562 undraw the trees(烂题) ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...

  5. uva 10305 ordering tasks(超级烂题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHIAAAHDCAYAAABI5T2bAAAgAElEQVR4nOydPY7svLW1awQGNABHCm

  6. UVA.10881 Piotr's Ants (思维题)

    UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...

  7. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  9. UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...

随机推荐

  1. Spring源码阅读-BeanFactory体系结构分析

    BeanFactory是Spring中非常重要的一个类,搞懂了它,你就知道了bean的初始化和摧毁过程,对于深入理解IOC有很大的帮助. BeanFactory体系结构 首先看一下使用IDEA生成的继 ...

  2. PTA 朋友圈 (25 分) 代码详解 (并查集)

    1.题目要求: 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友" ...

  3. kivy之Button常用属性实操练习

    kivy提供了Button按钮一系列属性来改变样式,下面列了常用的一些Button属性并用实操案例进行演练学习. 新建一个main.py,内容代码如下: from kivy.app import Ap ...

  4. 【Python机器学习实战】决策树和集成学习(一)

    摘要:本部分对决策树几种算法的原理及算法过程进行简要介绍,然后编写程序实现决策树算法,再根据Python自带机器学习包实现决策树算法,最后从决策树引申至集成学习相关内容. 1.决策树 决策树作为一种常 ...

  5. Redis应用场景及缓存问题

    1.应用场景 (1)   缓存 缓存机制几乎在所有的大型网站都有使用,合理地使用缓存不仅可以加快数据的访问速度,而且能够有效地降低后端数据源的压力.Redis 提供了键值过期时间设置,并且也提供了灵活 ...

  6. [1.1W字] 复习: CSS 9个背景属性&6种渐变函数, 学会可以手写实现AI中强大的"任意渐变"! #Archives009

    Title/ CSS Background&Gradient完全指南 #Archives009 序: 关于 background 属性, 了解点CSS的人总会知道个大概. 但是你肯定多半还有点 ...

  7. 在JavaScript中安全访问嵌套对象

    大多数情况下,当我们使用JavaScript时,我们将处理嵌套对象,并且通常我们需要安全地访问最里面的嵌套值. 比如: const user = { id: 101, email: 'jack@dev ...

  8. 编写一个简单的COM组件

    参考网站:编写一个简单的COM组件_a ray of sunshine-CSDN博客 (1) 用MIDL编写.idl文件 //将以下代码保存成 IXIYIZ.idl 文件 //在命令行上进行编译,编译 ...

  9. linux(5)----------防火墙的配置

    1.安装:    yum install firewalld 2.启动:    service firewalld start 3.检查状态:        service firewalld sta ...

  10. 使用dom4j工具:获取xml中的标签属性(三)

    package dom4j_read; import java.io.File; import java.util.List; import org.dom4j.Attribute; import o ...