我的创作纪念日

机缘

第一次接触csdn是因为看到baidu上很多代码都是来自于这里,没有账号就无法copy,所以就注册了一个账号。

由于我是一个表达欲很强的OIer,所以就开始慢慢写一些文章。


收获

其实没有什么收获,没有粉丝,没有太多的阅读量。但是我依然很开心,也会一直写下去。


日常

刷题->不会->看题解->哦↗↘->有意思->AC->写blog->发表


成就

提示:你过去写得最好的一段代码是什么? 请用代码块贴出来
题目:Code The Tree

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
string tree;
int main(){
	while(getline(cin,tree)){//The string probably have space
		//create a tree
		if(tree.find('(')==0&&tree.find(')')==tree.size()-1){
			puts("");
			continue;
		}
		else{
			vector<int>Tree[55];
			stack<int>num;//To save the numbers of nodes from the Tree
			int len=tree.size(),node=0/*to save how many nodes there are*/;
			for(int i=0;i<len-1/*Avoid exiting the stack when it is empty*/;){
				//save number of nodes
				if(/*is number*/tree[i]>='0'&&tree[i]<='9'&&i<len/*Avoid exceeding the length of the string*/){
					int temp=0;
					while(tree[i]>='0'&&tree[i]<='9'&&i<len){
						temp=temp*10+tree[i]-'0';
						i++;
					}
					node++;
					//push this number into the stack
					num.push(temp);
				}
				if(tree[i]=='('||tree[i]==' ')i++;//Omitted the char when it is "(" or space
				if(tree[i]==')'){//exiting the stack to push back into Tree the vector
					int n1=num.top();
					num.pop();//exiting
					int n2=num.top();//this node is not exit yet
					Tree[n1].push_back(n2);
					Tree[n2].push_back(n1);//creating a tree
					i++;//Don't forget
				}
			}
			//That is the end of this part
			for(int i=1;i<=node;i++){
				sort(Tree[i].begin(),Tree[i].end());
			}
			//
			vector<int>ans;
			int N=node-1;//There are N nodes need to delete
			while(N--){
				for(int i=1;i<=node;i++){
					int to;
					if(Tree[i].size()==1){
						//delete
						to=Tree[i][0];
						ans.push_back(to);
						Tree[i].resize(0);
						vector<int>::iterator pos;
						pos=lower_bound(Tree[to].begin(),Tree[to].end(),i);
						Tree[to].erase(pos);
						break;//each operate just delete one node
					}
				}
			}
			for(int i=0;i<ans.size();i++){
				//TODO
				if(i==0)cout<<ans[i];
				else cout<<" "<<ans[i];
			}
			cout<<endl;
		}
	}
	return 0;
}

憧憬

小目标: 参加NOI

Tips

  1. 您发布的文章将会展示至 里程碑专区 ,您也可以在 专区 内查看其他创作者的纪念日文章
  2. 优质的纪念文章将会获得神秘打赏哦
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值