Total Submission(s): 409 Accepted Submission(s): 202
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.
用一个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;
}
//这是我写的,既麻烦还不对(模拟太菜了):想了40多组数据终于找到了错误的地方了。。。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 710
#define M 100000000
using namespace std;
char s[300];
int main()
{
int i;
while(gets(s)!=NULL)
{
int l=strlen(s);
if(l==1)
{
if(s[0]>='p'&&s[0]<='z')
printf("YES\n");
else
printf("NO\n");
}
else if(s[l-1]>='C'&&s[l-1]<='N')
printf("NO\n");
else
{
int flag=0;int kk=0;int k=0;
for(i=l-1;i>=0;i--)
{
if(!((s[i]>='p'&&s[i]<='z')||s[i]=='C'||s[i]=='D'||s[i]=='E'||s[i]=='I'||s[i]=='N'))
{
flag=1;
break;
}
if(s[i]>='p'&&s[i]<='z')
{
k++;
continue;
}
else if(s[i]=='C'||s[i]=='D'||s[i]=='E'||s[i]=='I')
{
if(k>2||k+kk<2)
{
flag=1;
break;
}
else
{
k=1;
if(kk>1)
kk--;
}
}
else if(s[i]=='N')
{
if(k>1)
{
flag=1;
break;
}
else
{
if(k)
{
kk++;
k=0;
}
}
}
}
if(k>1)
flag=1;
if(flag)
printf("NO\n");
else
printf("YES\n");
}
}
return 0;
}
Ns
Cpp
CpCpp
NCpp
CpCpp
CCpNs
N
I
p
z
NCpCpCpp
NNNNs
NCpNsCpp
NNNCpCpNNsNsCpp
ppppp
pppNs
PCpp
CINs
NICps
INsNs
NssCpp
INsNsNs
INsCpp
CpCpCpCpCpCp
CpCpCpp
CpNsNs
CpCNsNs
CpCNsNsNs
CNNs
CNNNNs
CppCpp
ICCCppNsNsNs//这两组数据错了,
ICppNs