Gym - 100989J -(DFS)

题目链接:http://codeforces.com/gym/100989/problem/J

J. Objects Panel (A)
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Raihan is helping Maram and Master Hasan in the design of their graduation project. They have a panel that shows a list of the objects created by the user. Objects can be nested inside each other, the user can click the '-' sign to collapse the items nested inside an object, or click the '+' sign to expand the objects nested inside it.

Check the following table for more details:

 

Note that all objects are nested inside a list item called project.

For each object in the project, you are given a list of objects nested inside it and whether it’s expanded or collapsed. Can you help Raihan in drawing the panel?

Input

The first line of input contains an integer N (0 ≤ N ≤ 100), the number of objects in the project. Objects are numbered from 1 to N. The project item is item number 0.

Each of the following N + 1 lines contains the description of an object. The ith(starting from 0) line describes the object number i in the following format:

S K A1 A2 ... AK

Where S is the state of the object (expanded or collapsed), where '-' means it's expanded and '+' means it's collapsed. The state of objects that do not have any nested objects will be '-'.

K is the number of objects nested inside this object, this number is followed by Kdistinct numbers representing the numbers of the nested objects.

Output

Output the current state of the list, the items nested in an object should be listed in the given order.

An object inside another is nested by two spaces, the first space is replaced with '+' or '-' depending on the state of the object, if the object does not have other objects inside it, then keep the space.

Examples

Input
6
- 3 2 5 1
+ 1 3
- 1 6
- 1 4
- 0
- 0
- 0
Output
- project
- object2
object6
object5
+ object1
Input
0
- 0
Output
  project

题意:按照给出的项目要求,输出对象列表。

思路:因为他是按照先后顺序的,如果打开一个(输出)对象,接下来输出的是该嵌套在该对象中的其他对象,一直往下,直到到嵌套的最后一个对象,再依次返回上一层对象。所以很容易想到要用DFS

知道用DFS后这道题就不难了。注意一下输出格式,下面是代码。

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 char s[105];
 4 int father[105];//指project的子孩子有哪些
 5 int child[105][105];//child[i][j]指对象i的第j个child;
 6 int child_number[105];//child_number[i]指对象i的孩子有多少个
 7 void DFS(char s1,int childs,int i,int w)//(是'+'还是'-',孩子数,下标,缩放第几层次) 
 8 {
 9     for(int q=0;q<w;q++)
10     printf("  ");
11     if(childs==0)
12     {
13         printf("  object%d\n",i);
14         return ;
15     }
16     else
17     {
18         printf("%c ",s1);
19         printf("object%d\n",i);
20         if(s1=='-')//只有需要展开的时候才需要DFS
21         {
22             for(int j=1;j<=childs;j++)
23             {
24                 DFS(s[child[i][j]],child_number[child[i][j]],child[i][j],w+1);
25             }
26         }
27     }
28 }
29 int main()
30 {
31     int N;
32     while (scanf("%d",&N)!=EOF)
33     {
34         getchar();
35         memset(child,0,sizeof(child));
36         char s0;int k;
37         scanf("%c%d",&s0,&k);
38         for(int i=1;i<=k;i++)
39         scanf("%d",&father[i]);
40         for(int i=1;i<=N;i++)
41         {
42             getchar();
43             scanf("%c%d",&s[i],&child_number[i]);
44             for(int j=1;j<=child_number[i];j++)
45             scanf("%d",&child[i][j]);
46         }
47         if(N==0)
48         {
49             printf("  project\n");
50         }
51         else
52         {
53             if(s0=='+')
54             printf("%c project\n",s0);
55             else
56             {
57                 printf("%c project\n",s0);
58                 for(int i=1;i<=k;i++)
59                 {
60                     DFS(s[father[i]],child_number[father[i]],father[i],1);
61                 }
62             }
63         }
64     }
65     return 0;
66 }

 

 

 

转载于:https://www.cnblogs.com/bendandedaima/p/9277584.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/485706.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Leetcode--925. 长按键入

你的朋友正在使用键盘输入他的名字 name。偶尔&#xff0c;在键入字符 c 时&#xff0c;按键可能会被长按&#xff0c;而字符可能被输入 1 次或多次。 你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字&#xff08;其中一些字符可能被长按&#xff09;&…

艰难2020:人工智能的应用是否已停滞不前?

作者&#xff1a;Gary Grossman译者&#xff1a;Sambodhi策划&#xff1a;刘燕今年&#xff0c;每一个季度都是疯狂的一年&#xff0c;人工智能的发展同样如此。总的来说&#xff0c;这一年人工智能的发展喜忧参半&#xff0c;其中有显著的进展&#xff0c;也有对技术滥用的新发…

centos php日志分析,记录一下CentOS7安装GoAccess日志分析工具

之前项目一直都在用这个日志分析工具&#xff0c;自己也曾搭建过。现在整理一下之前的安装使用过程。GoAccess是一个开源的实时网络日志分析器和交互式查看器&#xff0c;可在Linux终端或浏览器中运行。为系统管理员提供可视化的服务器报告&#xff0c;为系统管理员提供快速且有…

浅谈WM算法

1. WM&#xff08;Wu-Manber&#xff09;算法的简单理解&#xff1a;&#xff08;1&#xff09;WM算法需要的参数&#xff1a;∑&#xff1a;字母集c&#xff1a; 字母集数目m&#xff1a;模式串集合中&#xff0c;字符串长度最小的模式串的长度B&#xff1a;字符块长度&#…

Leetcode--845. 数组中的最长山脉

我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”&#xff1a; B.length > 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] < ... B[i-1] < B[i] > B[i1] > ... > B[B.length - 1] &#xff08;注意&#xff1a;B 可以是 A 的任意子数…

AI研习丨专题:因果推断与因果性学习研究进展

来源&#xff1a;《中国人工智能学会通讯》2020年 第10卷 第5期 机器学习及其应用专题0 引言因果关系一直是人类认识世界的基本方式和现代科学的基石。爱因斯坦就曾指出&#xff0c;西方科学的发展是以希腊哲学家发明形式逻辑体系&#xff0c;以及通过系统的实验发现有可能找…

php api命名历史,PHP历史上的今天查询api源码

PHP历史上的今天查询API源码&#xff0c;本地新建PHP文件上传到服务器即可使用&#xff0c;非储存性&#xff0c;直接解析网址<?php function showjson($json){header("Access-Control-Allow-Origin:*");header(Content-type: application/json);exit(json_encod…

vuex调用接口

1、安装vue、vuex在main.js中引入 import Vue from vueimport store from ./vueximport server from ./server2、server中文件adapter.jsimport Axios from "axios";getServerList() {class Processor { constructor(result) { return result } }; return new Promis…

传感器的未来: 10年后我们将会生活在一个极端透明的世界

来源&#xff1a;大数据文摘作者&#xff1a;彼得戴曼迪斯2014年&#xff0c;在芬兰的一个传染病实验室里&#xff0c;卫生研究员佩特里拉特拉&#xff08;Petteri Lahtela&#xff09;发现了一件奇怪的事情&#xff0c;他突然意识到他所研究的很多问题的条件都存在着重叠。例如…

MySQL小问题:导入employee测试数据

在cmd窗口下输入&#xff1a;mysql -u root -p -t < employees.sql 会产生Unknown system variable storage_engine这个错误 解决方法&#xff1a; 在employees.sql文件中 把第38行的set storage_engine InnoDB; 改为 set default_storage_engine InnoDB; 把第44行…

企业计算机服务器中了babyk勒索病毒怎么办,babyk勒索病毒解密数据恢复

在数字化的今天&#xff0c;网络安全威胁不断增加&#xff0c;给企业的生产生活带来了严重影响&#xff0c;使得企业不得不重视数据安全问题。近日&#xff0c;云天数据恢复中心接到企业求助&#xff0c;企业的计算机服务器中了babyk勒索病毒&#xff0c;导致企业所有计算机系统…

java yied的用法,Java多线程的wait(),notify(),notifyAll()、sleep()和yield()方法使用详解,...

Java多线程的wait()&#xff0c;notify()&#xff0c;notifyAll()、sleep()和yield()方法使用详解&#xff0c;Java多线程中的wait()&#xff0c;notify()&#xff0c;notifyAll()、sleep()和yield()方法我们先从一个案例开始&#xff1a;static public class WaitingTest {//s…

ubuntu-server-18.04 设置开机启动脚本

ubuntu-16.10 开始不再使用initd管理系统&#xff0c;改用systemd systemd is now used for user sessions. System sessions had already been provided by systemd in previous Ubuntu releases. 快速看了 systemd 的使用方法&#xff0c;发现改动有点大&#xff0c; 包括用 …

海马体启发的记忆模型

来源&#xff1a;混沌巡洋舰 记忆是人类智能的关键&#xff0c;我们因为记忆可以把过去和当下整合成为一体&#xff0c; 并且可以预测未来。记忆不仅是一个信息承载的工具&#xff0c; 更是世界模型的本体&#xff0c; 它无时无刻不在刻画未来&#xff0c; 也被当下影响&#…

Leetcode--239. 滑动窗口最大值

给定一个数组 nums&#xff0c;有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回滑动窗口中的最大值。 示例: 输入: nums [1,3,-1,-3,5,3,6,7], 和 k 3 输出: [3,3,5,5,6,7] 解释: …

java中build是什么意思,Java中的Stream.Builder build()

Stream.Builder类的build()方法将构建流&#xff0c;并将此构建器转换为已构建状态。语法如下-Stream build()以下是实现Stream.Builder类的build()方法的示例-示例import java.util.stream.Stream;public class Demo {public static void main(String[] args) {Stream.Builder…

设置build.gradle打包时自动加时间

在build.gradle中添加以下函数&#xff1a; def releaseTime() {return new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("GMT08:00")) }引用这个函数&#xff1a; def fileName "SecyrityPassDemo_${defaultConfig.versionName}_${release…

Leetcode--283. 移动零

给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作&#xff0c;不能拷贝额外的数组。 尽量减少操作次数。 class Solution { publ…

DeepMind最新发现!神经网络的性能竟然优于神经符号模型

来源&#xff1a;深度学习这小事按照之前的常识&#xff0c;结合了算法和符号推理技术的神经符号模型&#xff08;Neurosymbolic Models&#xff09;&#xff0c;会比神经网络更适合于预测和解释任务&#xff0c;此外&#xff0c;神经符号模型在反事实方面表现更好。而Neural-S…

php搜索间隔,php – 如何查找超过2个用户的匹配时间间隔

要查找user1和user2都是免费的,请尝试以下操作&#xff1a;selecta.datetime_start as user1start,a.datetime_end as user1end,b.datetime_start as user2start,b.datetime_end as user2end,case when a.datetime_start > b.datetime_start then a.datetime_startelse b.da…