POJ 1126:Simply Syntax

原创 2015年07月08日 10:00:27

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(思维)

Simply Syntax Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5546   ...

Hdoj 1433 Simply Syntax 【string】

Simply Syntax Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

UVa 496 Simply Subsets (STL&set_intersection)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&p...

torch学习(五) simply application

一、getting started 1.torch/install/lib/lua/5.1/libjpeg.so: undefined symbol: jpeg_mem_src 解决方案:https:...

SM32x_I1126_v2.02.4

  • 2010-09-28 08:23
  • 3.18MB
  • 下载

BootStrap主题Simply Me的创建过程

首先看下该主题的总体效果 创建过程: 1.添加基本页面:  Bootstrap Theme Simply Me Who Am ...

Taking Photos Simply笔记

Taking Photos Simply笔记以下内容均是使用已经存在的carmera app进行拍照。Request Camera Permission如果拍照是你的app一个非常重要的功能,在man...
  • ws_lm
  • ws_lm
  • 2017-02-04 18:13
  • 140

MIUI 1126 通话杂音补丁

玲珑学院OJ 1126 - 咸鱼旅行【二分+并查集】

1126 - 咸鱼旅行 Time Limit:3s Memory Limit:128MByte Submissions:457Solved:76 DESCRIPTIO...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)