necu1454 http://acm.cs.ecnu.edu.cn/problem.php?problemid=1454
zzuli1203 http://202.196.6.170/JudgeOnline/showproblem?problem_id=1203
Description
Surely you know someone who thinks he is very clever. You decide to let him down with the following problem:
"Can you tell me what the syntax for a set is?", you ask him.
"Sure!", he replies, "a set encloses a possibly empty list of elements within two curly braces. Each element is either another set or a letter of the given alphabet. Elements in a list are separated by a comma."
"So if I give you a word, can you tell me if it is a syntactically correct representation of a set?"
"Of course, any fool can do it!" is his answer.
Now you got him! You present him with the following grammar, defining formally the syntax for a set (which was described informally by him):
Set ::= "{" Elementlist "}"
Elementlist ::= <empty> | List
List ::= Element | Element "," List
Element ::= Atom | Set
Atom ::= "{" | "}" | ","
<empty> stands for the empty word, i.e., the list in a set can be empty.
Soon he realizes that this task is much harder than he has thought, since the alphabet consists of the characters which are also used for the syntax of the set. So he claims that it is not possible to decide efficiently if a word consisting of "{", "}" and "," is a syntactically correct representation of a set or not.
To disprove him, you need to write an efficient program that will decide this problem.
Input
The first line of the input contains a number representing the number of lines to follow.
Each line consists of a word, for which your program has to decide if it is a syntactically correct representation of a set. You may assume that each word consists of between 1 and 200 characters from the set { "{", "}", "," }.
Output
Output for each test case whether the word is a set or not. Adhere to the format shown in the sample output.
Sample Input
4
{}
{{}}
{{}},{,}}
{,,}
Sample Output
Word #1: Set
Word #2: Set
Word #3: Set
Word #4: No Set
Source
Ulm Local 2005
这一题乍读上去感觉很难,有很多的情况要考虑。然后分析后就发现了方法--递归
就按照题目的意思对串s进行isset判断,程序都是按照题目描述set判断方法进行判断,也不包含什么技巧性了。
这一题加深了对递归的理解,以及函数间的相互调用,十分巧妙。
mom数组里存的是串从from到end对set的判断结果,mom2数组里存的是串从from到end对list的判断结果
代码
#include < string .h >
#define N 202
int mom[N][N],mom2[N][N];
char s[N];
int islist( int , int );
int isset( int from, int end)
{
if (mom[from][end] >= 0 )
return mom[from][end];
if (from >= end)
return mom[from][end] = 0 ;
if (s[from] != ' { ' || s[end] != ' } ' )
return mom[from][end] = 0 ;
if (from + 1 == end)
return mom[from][end] = 1 ;
else
return mom[from][end] = islist(from + 1 ,end - 1 );
}
int iselement( int from, int end)
{
if (from == end && (s[end] == ' { ' || s[end] == ' } ' || s[end] == ' , ' ))
return 1 ;
return isset(from,end);
}
int islist( int from, int end)
{
int k;
if (mom2[from][end] >= 0 )
return mom2[from][end];
if (iselement(from,end))
return mom2[from][end] = 1 ;
for (k = from + 1 ;k <= end;k ++ ){
if (s[k] == ' , ' )
if (iselement(from,k - 1 ) && islist(k + 1 ,end))
return mom2[from][end] = 1 ;
}
return mom2[from][end] = 0 ;
}
int main()
{
int T,i,j,k,len;
scanf( " %d " , & T);
getchar();
for (i = 1 ;i <= T;i ++ ){
gets(s);
len = strlen(s);
for (k = 0 ;k < len;k ++ ){
for (j = k;j < len;j ++ )
mom[k][j] = mom2[k][j] =- 1 ;
}
printf( " Word #%d: " ,i);
if (isset( 0 ,len - 1 ))
puts( " Set " );
else
puts( " No Set " );
}
return 0 ;
}
本文介绍了一道关于递归解析集合语法的编程题。通过对题目描述的深入理解,采用递归的方法来判断一个由特定字符组成的字符串是否符合集合的语法规范。文章详细解释了解决方案的实现过程,并提供了一个简洁高效的程序示例。

被折叠的 条评论
为什么被折叠?



