CodeForces - 190C (2020.4.2训练K题)

Problem
Vasya used to be an accountant before the war began and he is one of the few who knows how to operate a computer, so he was assigned as the programmer.

We all know that programs often store sets of integers. For example, if we have a problem about a weighted directed graph, its edge can be represented by three integers: the number of the starting vertex, the number of the final vertex and the edge’s weight. So, as Vasya was trying to represent characteristics of a recently invented robot in his program, he faced the following problem.

Vasya is not a programmer, so he asked his friend Gena, what the convenient way to store n integers is. Gena used to code in language X-- and so he can use only the types that occur in this language. Let’s define, what a “type” is in language X–:

First, a type is a string “int”.
Second, a type is a string that starts with “pair”, then followed by angle brackets listing exactly two comma-separated other types of language X–. This record contains no spaces.
No other strings can be regarded as types.
More formally: type := int | pair<type,type>. For example, Gena uses the following type for graph edges: pair<int,pair<int,int>>.

Gena was pleased to help Vasya, he dictated to Vasya a type of language X–, that stores n integers. Unfortunately, Gena was in a hurry, so he omitted the punctuation. Now Gena has already left and Vasya can’t find the correct punctuation, resulting in a type of language X–, however hard he tries.

Help Vasya and add the punctuation marks so as to receive the valid type of language X–. Otherwise say that the task is impossible to perform.

Input
The first line contains a single integer n (1 ≤ n ≤ 105), showing how many numbers the type dictated by Gena contains.

The second line contains space-separated words, said by Gena. Each of them is either “pair” or “int” (without the quotes).

It is guaranteed that the total number of words does not exceed 105 and that among all the words that Gena said, there are exactly n words “int”.

Output
If it is possible to add the punctuation marks so as to get a correct type of language X-- as a result, print a single line that represents the resulting type. Otherwise, print “Error occurred” (without the quotes). Inside the record of a type should not be any extra spaces and other characters.

It is guaranteed that if such type exists, then it is unique.

Note that you should print the type dictated by Gena (if such type exists) and not any type that can contain n values.

题意:背景围绕两个数据类型pair和int,给出int的个数,再给出一个字符串,每两个单词间以空格做间距,按字符串里出现的顺序并按pair的特性存储int,如能正好存储,则输出存储的形式串,不能则输出Error occurred

这题要用到神奇的stringstream和getline,通过这题彻底我搞懂了getline,其实最主要的就这两个,其他的就是模拟了,注意一些细节就好了

AC代码:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
stringstream ss, ans;
string s;
bool op()
{
	if (ss.eof()) 
		return false;
	ss >> s;
	if (s == "pair")
	{
		ans << "pair";
		ans << "<";
		if (!op()) 
			return false;
		ans << ",";
		if (!op()) 
			return false;
		ans << ">";
	}
	else 
		ans << "int";
	return true;
}
int main()
{
	getline(cin, s);	
	getline(cin, s);
	ss << s;
	if (op() && ss.eof())
		cout << ans.str() << endl;
	else
		cout << "Error occurred" << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值