机缘
第一次接触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