首页 > 其他分享 >hdoj Simply Syntax 1433 (模拟)

hdoj Simply Syntax 1433 (模拟)

时间:2023-04-19 15:35:51浏览次数:41  
标签:cou 1433 sentence int Syntax break Simply include 句子

Simply Syntax Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 409    Accepted Submission(s): 202


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 //题意: 判断一个句子是否是合格的有4个条件: 0、句子中只包含字母p---z和C,D,E,I,N; 1、p---z每个字母是一个句子(关键性条件); 2、如果句子s是正确的,那么Ns是正确的; 3、如果句子s、t是正确的,那么Cst,Dst,Est,Ist是正确的; 4、判断句子正确的条件只有0--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;
}
//这是我写的,既麻烦还不对(模拟太菜了):想了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




标签:cou,1433,sentence,int,Syntax,break,Simply,include,句子
From: https://blog.51cto.com/u_16079508/6206434

相关文章

  • [oeasy]python0135_python_语义分析_ast_抽象语法树_abstract_syntax_tree
    语义分析_抽象语法树_反汇编回忆上次回顾了一下历史python是如何从无到有的看到Guido长期的坚持和努力python究竟是如何理解print("hello")的?这些ascii字母如何被组织起来执行?纯文本首先编写Guido的简历print("1982------Guidoincwi")print("1995------Guidoincnri")pri......
  • [oeasy]python0135_python_语义分析_ast_抽象语法树_abstract_syntax_tree
    语义分析_抽象语法树_反汇编回忆上次回顾了一下历史python是如何从无到有的看到Guido长期的坚持和努力 ​ 添加图片注释,不超过140字(可选) python究竟是如何理解print("hello")的?这些ascii字母如何被组织起来执行? ......
  • 储存数据至mysql数据库时出现 (1064, "You have an error in your SQL syntax; check
      在msyql数据库中存储数据时,程序出现了如下报错:  打印存储的数据类型发现数据类型有错误,将数据转为str类型就可以了。。。解决思路:  在初入数据库学习时,出现这个报错还是有些懵的,于是改了捕获异常,发现存储数据函数有问题。从报错中可以看出是有跟'自营店'类似的数据有......
  • adobe安装提示错误“Error:SyntaxError:JSON Parse error:Unexpected EOF”的解决方法
    mac电脑安装Adobe时,会提示错误“Error:SyntaxError:JSONParseerror:UnexpectedEOF”,这是怎么回事儿的,不管您是安装AI、ps、PR还是lr,如果也遇到相同的问题,可以参考一下方法解决。adobe安装提示错误“Error:SyntaxError:JSONParseerror:UnexpectedEOF”的解决方法,如下:需要使用A......
  • Java: switch lambda-like syntax
     Theswitchexpressionhasanadditionallambda-likesyntaxanditcanbeusednotonlyasastatement,butalsoasanexpressionthatevaluatestoasinglevalue. Withthenewlambda-likesyntax,ifalabelismatched,thenonlytheexpressionorstat......
  • SyntaxError: Non-UTF-8 code starting with ‘\xb9‘ in file问题的解决
    问题描述按照正常模式运行python代码,其中涉及到charset类型,由于某种原因导致代码运行不成功问题解决在python程序的第一行加上这样一行代码:#coding=gbk这样的话,该问题就能解决啦!......
  • wp-Syntax 插件使用方法
    一、使用方法1、语法结构为<prelang=”LANGUAGE”line=”1″>要插入的代码</pre>,此语言是一种称作GeSHi支持的语言的语法。2、由于在可视化编辑状态下,<>在html下却是转译符&amp;lt;,则HTML等有转译字符的使用<prelang=”LANGUAGE”line=”1″escaped=”true”>要插入的代......
  • SyntaxError: Non-UTF-8 code starting with ‘\xb2‘ in file xxx.py but no encodi
    openCV系列文章目录文章目录openCV系列文章目录前言一、问题原因二、解决办法1.点击“运行按钮”->RunPythonfile前言#coding=gbkimportcv2importnumpyasnpdefmouse_callback(event,x,y,flags,userData):print(event,x,y,flags,userData)#mouse_callb......
  • SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too lo
    PSD:\Centos\PHPstudyWWW\dcatAdmin>phpartisanadmin:installMigrationtablecreatedsuccessfully.Migrating:2014_10_12_000000_create_users_tableIllumina......
  • SQL Server开启1433端口,彻底解决方案
    SQLServer开启1433端口,彻底解决方案yaohan404关注IP属地:广东0.1782018.01.0510:34:18字数377阅读29,057环境:Windows10+SQLServer2008在用JDBC连接SQLSe......