题目一
白给
题目二
哭了,第二题就直接开始面壁罚坐~~
这是需要通过前序遍历二叉树的思路来解决的
因为一个pair必有一个左子树和一个右子树,此时子树内的东西不确定,但是一定会有左右子树。
通过递归进行二叉树的构建,在构建的过程中加入相应的符号。
代码
#include<iostream>
#include<cstring>
using namespace std;
string ans;
bool dfs()
{
char ch[5];
if(scanf("%s",ch)==-1)return false;//
if(!strcmp("pair",ch))//比较数据
{
ans+=ch;//将pair数组加入到ans中
ans+="<";
if(!dfs())return false;//左子树递归
ans+=",";
if(!dfs())return false;//右子树递归
ans+=">";
}
else ans+=ch;//直接加入到
return true;
}
int main()
{
scanf("%*d");//跳过录入字符
if(dfs()&&scanf("%*s")==-1)//完整录入
{
puts(ans.c_str());
}
else puts("Error occurred");
return 0;
}
题目三
有时间就写