Simply Syntax
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5546 | Accepted: 2480 |
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.
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
Source
题目大意:p~z的单个字母是一个句子,N+一个句子还是一个句子,C\D\E\I后面得加两个句子才构成一个句子,这里的句子能嵌套。给你一个字符串,问是否最终是一个句子。(这题上说保证属如不含其他字符,但是不是的。。。)
解题思路:从后往前扫,用一个num记录当前句子个数,一旦不符合规则就跳出循环。
代码如下:
#include <cstdio>
#include <cstring>
char s[300];
int main()
{
while(scanf("%s",s)!=EOF)
{
int size=strlen(s);
size--;
int num=0;//记录当前句子个数
int i=size;
while(i>=0)
{
if(s[i]>='p'&&s[i]<='z')
{
num++;
}
else
{
if(s[i]=='C'||s[i]=='D'||s[i]=='E'||s[i]=='I')
{
num--;// 比如原来有两个句子st,那么遇到C D E I的话变成Cst 是一个句子 ,相当于是2+1-2=2-1=1个句子了 2+1是多了一个句子 -2是少了2个句子
if(num<1)//说明原先不足两个句子,那么遇到这种情况不能组成一个句子
{
break;
}
}
else
{
if(s[i]=='N')//这里不用num-- 相当于num+1-1 多了一个N开头的句子,少了一个和N匹配的句子
{
if(num<1)//原先不足一个句子和N匹配不成新的句子
{
break;
}
}
else//出现别的字符了,那么break
{
num=-1;
break;
}
}
}
i--;
}
if(num==1)//只有当最后是一个句子的时候才输出yes
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}