校赛4

Thread Tree 
Nathan O. Davis has been running an electronic bulletin board system named JAG-channel. He is now having hard time to add a new feature there --- threaded view. 


Like many other bulletin board systems, JAG-channel is thread-based. Here a thread (also called a topic) refers to a single conversation with a collection of posts. Each post can be an opening post, which initiates a new thread, or a reply to a previous post in an existing thread. 


Threaded view is a tree-like view that reflects the logical reply structure among the posts: each post forms a node of the tree and contains its replies as its subnodes in the chronological order (i.e. older replies precede newer ones). Note that a post along with its direct and indirect replies forms a subtree as a whole. 


Let us take an example. Suppose: a user made an opening post with a message hoge; another user replied to it with fuga; yet another user also replied to the opening post with piyo; someone else replied to the second post (i.e. fuga”) with foobar; and the fifth user replied to the same post with jagjag. The tree of this thread would look like: 


hoge
├─fuga
│ ├─foobar
│ └─jagjag
└─piyo


For easier implementation, Nathan is thinking of a simpler format: the depth of each post from the opening post is represented by dots. Each reply gets one more dot than its parent post. The tree of the above thread would then look like: 


hoge
.fuga
..foobar
..jagjag
.piyo


Your task in this problem is to help Nathan by writing a program that prints a tree in the Nathan's format for the given posts in a single thread. 




 
Input 
Input contains a single dataset in the following format: 


n
k_1
M_1
k_2
M_2
:
:
k_n
M_n


The first line contains an integer n (1 \leq n \leq 1,000), which is the number of posts in the thread. Then 2n lines follow. Each post is represented by two lines: the first line contains an integer k_i (k_1 = 0, 1 \leq k_i < i for 2 \leq i \leq n) and indicates the i-th post is a reply to the k_i-th post; the second line contains a string M_i and represents the message of the i-th post. k_1 is always 0, which means the first post is not replying to any other post, i.e. it is an opening post. 


Each message contains 1 to 50 characters, consisting of uppercase, lowercase, and numeric letters. 
其实就是找一下规律,然后建立子节点,运用递归函数,递归输出即可 
 



#include<iostream>//A了 
#include<cmath>
#include<cstring>
#define maxn 1002
using namespace std;
char str[maxn][60];
int child[maxn];
int dp[maxn][maxn];
int dot[maxn];
int t;
int x;
	
void outchild(int x)
{
    
	for(int k=1;k<=child[x];k++)
	{	
	    for(int j=0;j<dot[dp[x][k]];j++)
	    {
	    cout<<".";
		
	    }
		cout<<str[dp[x][k]]<<endl;
		outchild(dp[x][k]);
	}
	return;
}    


int main()
{
	while(~scanf("%d",&t))
	{
		memset(child,0,sizeof(child));
		
		dot[0]=-1;
		for(int i=1;i<=t;i++)
		{
			cin>>x>>str[i];
			dot[i]=dot[x]+1;
			child[x]++;
			dp[x][child[x]]=i;
		
		}
		
	   cout<<str[1]<<endl;
	  
	   outchild(1);
	   
	   }
	   

	return 0;

}

	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值