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<stdio.h>
#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;
}

转载于:https://www.cnblogs.com/DreamUp/archive/2010/07/10/1774990.html

Any fool can do it (递归)相关推荐

  1. 算法设计思想(5)— 递归法

    1. 递归概念 递归 Recursion是指在函数的定义中使用函数自身的方法,直观上来看,就是某个函数自己调用自己. ​ 递归有两层含义: 递归问题必须可以分解为若干个规模较小.与原问题形式相同的子问 ...

  2. 判断某数组是不是二叉树的后序遍历序列 python递归与非递归解法

    python 递归 class Solution:def VerifySquenceOfBST(self, sequence):# write code hereif len(sequence) &l ...

  3. 翻转二叉树 c语言实现 递归 栈 队列

    前言 题目比较好理解,就是翻转二叉树 代码 c语言实现 #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  4. 快速排序的递归和非递归实现 c语言版本

    代码 挖坑法 解释 选取一个关键字(key)作为枢轴,一般取整组记录的第一个数/最后一个,这里采用选取序列第一个数为枢轴,也是初始的坑位. 设置两个变量i = l;j = r;其中l = 0, r = ...

  5. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  6. 迭代是人,递归是神(迭代与递归的总结:比较)

    https://www.cnblogs.com/Renyi-Fan/p/7708012.html 在计算机编程实现中有常常两种方法:一曰迭代(iterate):二曰递归(recursion). 从&q ...

  7. java锁(公平锁和非公平锁、可重入锁(又名递归锁)、自旋锁、独占锁(写)/共享锁(读)/互斥锁、读写锁)

    前言 本文对Java的一些锁的概念和实现做个整理,涉及:公平锁和非公平锁.可重入锁(又名递归锁).自旋锁.独占锁(写)/共享锁(读)/互斥锁.读写锁 公平锁和非公平锁 概念 公平锁是指多个线程按照申请 ...

  8. Mysql中的递归层次查询(父子查询,无限极查询)

    前言:最近面试的时候遇到公司要求只能用SQL来查询无限极的数据,不能用PHP程序做递归查询,现在分享方法. 下面给出一个function来完成的方法 下面是sql脚本,想要运行的直接赋值粘贴进数据库即 ...

  9. [递归]一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

    这题是用C写的~ 在牛客上半天找不着ACM模式,练习模式里只有核心代码模式 这样用C语言编译器就不能自定义函数啊,不鸡肋吗??? 解决方法:在核心代码模式下用C++编译器(反正C++完全兼容C的不是吗 ...

最新文章

  1. mongo在哪创建管理员_MongoDB初始化创建管理员账户登录
  2. springmvc的执行流程_springmvc执行流程
  3. 记录下面试中的回答的不好的问题
  4. OpenCV--常见图片格式转换与深浅拷贝
  5. 坚果云开发团队分享高效代码审查经验
  6. jsp加载常量的探讨
  7. 如何为网站进行安装SSL证书
  8. Android进阶--android自动化测试python+uiautomator
  9. SolidWorks设计助手,可以标注和实体无关的工程图标注
  10. 苹果 iPhone 13 内置原生壁纸下载
  11. 为什么买域名必须实名认证?这样做什么原因?
  12. ubuntu18.0.4 无线网卡无法上网解决!
  13. 如何将MathType公式编辑器内嵌到WPS工具栏中
  14. java根据经纬度获取地址
  15. Java框架jboot_微服务框架 Jboot 2.0.5 发布,常规更新
  16. 黑苹果 MAC Monterey 在睡眠后 bluetoothd 占用很高的cpu解决方案
  17. 【C语言】输出国际象棋棋盘
  18. 卡莱特led显示屏调试教程_卡莱特5A接收卡调屏步骤.doc
  19. 安装linux无法进入图形界面,centos7安装进不去图形界面
  20. Macy‘s Thanksgiving Day Parade

热门文章

  1. 如何将页面m3u8、blob类型的视频链接下载下来?
  2. C语言考试系统选题代码,java毕业设计_springboot框架的C语言考试系统
  3. SpringBoot优雅退出
  4. 浙江大学计算机学院研究生院教师研究方向2010
  5. C语言试题练习【01】【解析】
  6. 达芬奇pro的FPGA学习笔记0--对自己想说的话以及之后的项目规划
  7. Idioms about music
  8. Java微信支付开发之公众号支付(微信内H5调起支付)
  9. 基于FME实现的地理数据库批量建库的解决方案,支持gdb、mdb、shapefile等数据格式,gdb批量建库,mdb批量建库,shp批量建库,shapefile批量建库,地理数据批量建库
  10. ata考证和计算机一级b,ATA