Simply Syntax
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5618 | Accepted: 2510 |
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
题意很简单,按照题目说的4个规则判断输入的字符串是不是一个句子
思路:直接从后往前扫,sum为句子数量,遇到p~z sum++,遇到N,判断当前句子数量是否大于1,小于1 就break,遇到C D E I判断sum是否大于2,小于2break,另外,如果出现了其他字符,break;到最后sum为1输出YES,否则输出NO
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
int n,m;
int par[1005],_rank[1005];
int edge[105][105];
char str[280];
int main()
{
while(~scanf("%s",str))
{
int len=strlen(str);
int sum=0;
int flag=1;
for(int i=len-1;i>=0;i--){
if(str[i]>='p'&&str[i]<='z')
sum++;
else if(str[i]=='N'){
if(sum<1){
flag=0;break;
}
}
else if(str[i]=='C'||str[i]=='D'||str[i]=='E'||str[i]=='I'){
if(sum<2){
flag=0;break;
}
sum--;
}
else{
flag=0;break;
}
}
if(flag&&sum==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}