码迷,mamicode.com
首页 > 其他好文 > 详细

Hdoj 1433 Simply Syntax 【string】

时间:2015-04-27 23:45:04      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:字符串

Simply Syntax

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 320    Accepted Submission(s): 166


Problem 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每个字母都是合法的句子

1, 如s是一个合法的句子,那么Ns也是一个合法的句子

2,如果s与t都是合法的一个句子,那么C, D, E, and I.

3,只有满足以上的才是一个合法的句子


我们倒着来判断,用一个cou表示当前有几个合法的句子 最后判断是不是仅有一个合法的句子。

 注意当搜索到N的时候cou一定要为1.否则break

如果搜索到CDEI的时候cou < 2 那么也要break

还有可能出现不是上述的字母也要break

代码:

#include <cstdio>
#include <cstring>
const int M = 300;
using namespace std;

char s[M];

int main(){
	while(scanf("%s", s) == 1){
		int len = strlen(s);
		int cou = 0;
		for(int i = len-1; i >= 0; -- i){
			if(s[i] >= 'p'&&s[i] <= 'z'){
				++cou;
			}
			else if(s[i] == 'N'){
				if(cou == 0){
					break;
				}
			}
			else if(s[i] == 'C'||s[i] == 'D'||s[i] == 'E'||s[i] == 'I'){
				if(cou >= 2) --cou;
				else{
					cou = 0; break;
				}
			}
			else {
				cou = 0; break;
			}
		}
		if(cou == 1) printf("YES\n");
		else puts("NO");
	}
	return 0;
}


Hdoj 1433 Simply Syntax 【string】

标签:字符串

原文地址:http://blog.csdn.net/shengweisong/article/details/45318323

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!