Simply Syntax
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5264   Accepted: 2337

Description

In the land of Hedonia the official language is Hedonian. A Hedonian professor had noticed that many of her students still did not master the syntax of Hedonian well. Tired of correcting the many syntactical mistakes, she decided to challenge the students and
asked them to write a program that could check the syntactical correctness of any sentence they wrote. Similar to the nature of Hedonians, the syntax of Hedonian is also pleasantly simple. Here are the rules: 





0.The only characters in the language are the characters p through z and N, C, D, E, and I. 



1.Every character from p through z is a correct sentence. 



2.If s is a correct sentence, then so is Ns. 



3.If s and t are correct sentences, then so are Cst, Dst, Est and Ist. 



4.Rules 0. to 3. are the only rules to determine the syntactical correctness of a sentence. 



You are asked to write a program that checks if sentences satisfy the syntax rules given in Rule 0. - Rule 4.

Input

The input consists of a number of sentences consisting only of characters p through z and N, C, D, E, and I. Each sentence is ended by a new-line character. The collection of sentences is terminated by the end-of-file character. If necessary, you may assume
that each sentence has at most 256 characters and at least 1 character.

Output

The output consists of the answers YES for each well-formed sentence and NO for each not-well-formed sentence. The answers are given in the same order as the sentences. Each answer is followed by a new-line character, and the list of answers is followed by
an end-of-file character.

Sample Input

Cp
Isz
NIsz
Cqpq

Sample Output

NO
YES
YES
NO

题意是判断所给出的字符串是否符合题目中所给出的五项标准。

第0项:句子中的字母只能是p到z,和N、C、D、E、I。

第1项:每一个p到z的字母就单独是一个正确的句子。(这个其实很关键,标志了到底有几个句子,就是有多少个这样的小写字母就有多少个正确句子,一开始我就是没怎么仔细看第一个条件导致对第四个样例输出结果有疑惑。)

第2项:如果s是一个正确句子,那么Ns也是一个正确句子。

第3项:如果s和t是正确句子,那么Cst,Dst,Est,Ist也是正确句子。

第4项:只有规则0到3是判断句子是否正确的规则,其余的不算数。

输出只能是一个正确句子,两个以上的不行。

比方说sz。

因为s是一个正确句子,z也是一个正确句子,这样的话sz就有两个了,除非前面有N、C、D、E、I,变成一个,否则不行。

首先说一下:The input consists of a number of sentences consisting only of characters p through z and N, C, D, E, and I. 尽管是有这句话,但还是要考虑有其他字符出现的情况,可能这里会跪掉。

一开始想拿到一个字符串的话,扫描其字符有两种方式,从左至右或者是从右至左。然后自己想从左至右的话是怎么弄怎么麻烦,因为碰到C、D、E、I还要记录吃掉后面的两个句子,这样碰到要标记再碰到再标记。就觉得很麻烦,不如从右至左,遇到一个q到z,句子flag就加一。遇到N,flag不变。遇到N、C、D、E、I就flag减一。遇到其他字符的flag直接赋为0。最后看flag是否等于1,不等于的就No。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; int flag; void cal(string s)
{
if(s.length() == 0)
{
return;
}
else
{
int len=s.length();
if(s[len-1]>='p' && s[len-1]<='z')
{
flag++;
cal(s.substr(0,len-1));
}
else if(s[len-1]=='N')
{
cal(s.substr(0,len-1));
}
else if(s[len-1]=='C'||s[len-1]=='D'||s[len-1]=='E'||s[len-1]=='I')
{
flag--;
cal(s.substr(0,len-1));
}
else
{
flag=0;
return;
}
}
} int main()
{
string s;
while(cin>>s)
{
flag=0; cal(s); if(flag==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1126:Simply Syntax的更多相关文章

  1. Simply Syntax(思维)

    Simply Syntax Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5551   Accepted: 2481 Des ...

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  4. 【九度OJ】题目1126:打印极值点下标 解题报告

    [九度OJ]题目1126:打印极值点下标 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1126 题目描述: 在 ...

  5. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  8. POJ 3281:Dining(最大流)

    http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...

  9. POJ 3580:SuperMemo(Splay)

    http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...

随机推荐

  1. 原生JS获取所有标签的数量并统计每个标签的数量

    <script type="text/javascript"> var tags = document.getElementsByTagName('*'); var t ...

  2. Day9 - C - Bookshelf 2 POJ - 3628

    Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled u ...

  3. PHP如何查找一列有序数组是否包含某值(二分查找)

    问题:对于一列有序数组,如何判断给出的一个值,该值是否存在于数组. 思路:判断是否存在,最简单是,直接循环该数组,对每一个值进行比较.但是对于有序数组来说,这样写就完全没有利用好“有序”这一特点. 所 ...

  4. DDD-领域驱动设计之领域模型

    DDD领域驱动设计基本理论知识总结 Posted on 2011-10-10 01:01 netfocus 阅读(120434) 评论(82) 编辑 收藏 领域驱动设计之领域模型 加一个导航,关于如何 ...

  5. SIAMATIC S7-1200 中通过 Modbus RTU 如何读取地址范围 9999 到 65535 的输入字

    原文地址 说明 除了需要 STEP 7 >= V13 SP1 (TIA Portal) 的软件,还需要 S7-1200 CPU 固件版本 >= V4 (文章编号: 6ES721x-1xx4 ...

  6. sklearn中调用PCA算法

    sklearn中调用PCA算法 PCA算法是一种数据降维的方法,它可以对于数据进行维度降低,实现提高数据计算和训练的效率,而不丢失数据的重要信息,其sklearn中调用PCA算法的具体操作和代码如下所 ...

  7. SciPy 输入输出

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  8. CodeForces - 862C Mahmoud and Ehab and the xor(构造)

    题意:要求构造一个n个数的序列,要求n个数互不相同,且异或结果为x. 分析: 1.因为0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ (0 ^ 1 ^ 2 ^ 3 ...

  9. 图像检索:CEDD(Color and Edge Directivity Descriptor)算法 颜色和边缘的方向性描述符

    颜色和边缘的方向性描述符(Color and Edge Directivity Descriptor,CEDD) 本文节选自论文<Android手机上图像分类技术的研究>. CEDD具有抽 ...

  10. Day6 - D - Tree 园丁的烦恼 HYSBZ - 1935

    很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思索一个问题, ...