G.Generalized German Quotation---栈的简单运用

Generalized German Quotation

Time Limit: 3 Sec Memory Limit: 512 Mb

题目链接http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2312

Description

Generalized German Quotationstandard inputstandard output3 seconds512 megabytes

German language uses conventional angular quote marks (’<<’ and ‘>>’), so one may quote text in a <> way. What is unconventional in German is that one may also quote text in a >>reversed<< way. In normal life these styles do not mix, since they are used in different German-speaking countries. But let us have some fun! If we merge these two typographical traditions and forget about rules for nested quotes (that is, if we allow unlimited nesting), we will receive Generalized German rules that allow us to write small quotation masterpieces like this:

<<>>Anführungszeichen<< means <> in German>>

Informally we will say that a string is a correct quotation if it can be obtained by removing all non-quote characters from a correctly formed Generalized German text. Formally:

⟨G⟩: := ε | ⟨G⟩⟨G⟩ | ‘<<’⟨G⟩‘>>’ | ‘>>’⟨G⟩‘<<’

Thus, a correct quotation is an empty string, a concatenation of two correct quotations, or a correct quotation quoted in either a conventional or a reversed way. In the latter case, we will say that the quote mark to the left of ⟨G⟩ is a starting quote, and the quote mark to the right of ⟨G⟩ is an ending quote. For example, in quotation string ‘<<>>’ the quote mark ‘<<’ is a starting quote, while in string ‘>><<’ the same quote mark ‘<<’ is an ending quote.

Your task is to check whether the given string is a correct quotation, and if it is, restore its structure — that is, replace all starting quote marks with ‘[’ and all ending quote marks with ‘]’.

Input

The first and only line of the input contains a single string with a sequence of quote marks. To limit ourselves to plain ASCII, the quote marks ‘<<’ and ‘>>’ are encoded as ‘<<’ and ‘>>’, respectively. The string does not contain any other characters. The string is not empty and is not longer than 254 ASCII characters.

Output

If the input string is a correct quotation, replace all starting quote marks with ‘[’, all ending quote marks with ‘]’, and output the result. If there is more than one possible solution, output any of them.

If the string is not a correct quotation, output “Keine Loesung”.

Sample Input

<<>><<<<>>>>

Sample Output

[][[]]


emmm,题目大意:给你一串的半块书名号,让你判断它是否配对,如果可以则用[]进行替换。

很简单的一道题,数据还有点水。。
我们可以先将书名号进行替换,这样就变成了单个字符,我们就可以直接用head从头开始指了,碰到[ 就+1,碰到] 就-1,最后判断是否为零就行了。。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;
char s[300];
char stk[300];
int main()
{
	scanf ("%s",s);
	int len=strlen(s);
	int cnt=0;
	for (int i=0; i<len; i++){
		if (s[i]=='<') stk[cnt++]='[',i++;
		else if (s[i]=='>') stk[cnt++]=']',i++; 
	}
	stk[cnt]='\0';
	len=strlen(stk);
	int head=0;
	for (int i=0; i<len; i++){
		if (stk[i]=='[') head++;
		else head--;
	}
	if (head==0) printf ("%s",stk);
	else printf ("Keine Loesung\n"); 
	return 0;
}

当然这样可以过,但对于
<<>><<<<>>>>>><<它的回答依然是成立的。。。所以我们需要在中途判断head是否为负就可以了。。。详情请参考此题 https://www.luogu.org/problemnew/show/P1739

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值