STL CodeForces - 190C

原题链接
考察:dfs
错误思路:
  有点类似\(acwing\)的正则问题,然而并不是....
正确思路:
  当遇到一个pair后面一定是<,但是\(int\)不一定.需要考虑它在pair后的哪个位置.很巧妙的方法是一个pair< , >三个符号\(2\)\(dfs\).,是第一个dfs回溯后添加,>是第二个dfs后添加.

Code

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int N = 100010;
int n,cnt,i;
string s,ans;
bool ok;
void dfs()
{
	if(cin>>s)
	{
		ans+=s;
		if(s=="pair")
		{
			ans+="<";
			dfs();
			ans+=",";
			dfs();
			ans+=">";
		}
	}
}
int main()
{
	scanf("%d",&n);
	dfs();
	if(cin>>s||ans.find("<,")!=ans.npos||ans.find(",>")!=ans.npos) puts("Error occurred");
	else printf("%s\n",ans.c_str());
	return 0;
}
posted @ 2021-08-24 23:44  acmloser  阅读(44)  评论(0编辑  收藏  举报