Simply Syntax
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 416 Accepted Submission(s): 208
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.
//就是統計這個字符串中有多少個句子,如果只有一個就輸出YES,否則輸出NO!
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char a[256+10];
int main()
{
while(scanf("%s",a)!=EOF)
{
int cou=0,len=strlen(a);
for(int i=len-1;i>=0;i--)
{
if(a[i]>='p'&&a[i]<='z')
cou++;
else if(a[i]=='N')
{
if(cou==0)
{
break;
}
}
else if(a[i]=='C'||a[i]=='D'||a[i]=='E'||a[i]=='I')
{
if(cou>=2)
{
cou--;
}
else
{
cou=0;break;
}
}
else
{
cou=0;break;
}
}
if(cou==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}