ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-05-02 20:50:56  阅读:465  来源: 互联网

标签:string German quote Generalized marks quotation correct 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

标签:string,German,quote,Generalized,marks,quotation,correct,Quotation
来源: https://blog.csdn.net/qq_43906000/article/details/89764489

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有