ACM ICPC East Central North America 1994_Simply Syntax

本文介绍了一个用于检查特定ACM题目语法正确性的程序。该程序能够解析由字符p到z及N、C、D、E、I组成的句子,并判断其是否符合给定的语法规则。代码采用递归方式寻找句子的有效结束位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  写这篇博客,主要是贴一段代码。该代码用于解决哈尔滨工业大学(我的母校)acm上的一道题目。闲话少叙:

  代码中含有题目要求,代码如下:

/*
      Author : Li Pan
      Date : 2013/2/25
      Function : A program to solve an acm program named Simply Syntax. Its requirements is as follows : 
            	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.
  */


#include <stdio.h>

char test_string[1024];
char yes_or_no;

int get_last_index_of_sentence(int start) {
	
    char current_char;
    int last_index;
    int temp_index;
    if (start >= strlen(test_string)) {
	last_index = -1;
	return last_index;
    } 
	
    current_char = test_string[start];		
    switch(current_char) {
        case        'p' : 
	case 	'q' : 
	case      	 'r' : 
	case 	's' : 
	case 	't' : 
	case 	'u' : 
	case 	'v' : 
	case 	'w' : 
	case 	'x' : 
	case 	'y' : 
	case 	'z' : 
		
		last_index = start;		
	break;

	case 	'N' : 
		last_index = get_last_index_of_sentence(start + 1);
	break;
	
	case 	'C' : 
	case 	'D' : 
	case 	'E' : 
	case 	'I' :
		temp_index = get_last_index_of_sentence(start + 1);
		last_index = get_last_index_of_sentence(temp_index + 1);
	break;
	default : 
		/*very important for forbidden characters.*/
		last_index = -1;
	break;
	
    }
    return last_index;
}

int main() {

	int i;
	int last_index;
	fscanf(stdin, "%s", test_string);
	last_index = get_last_index_of_sentence(0);

	if (-1 == last_index)
		fprintf(stdout, "no\n");
	else if (last_index < strlen(test_string) - 1)
		fprintf(stdout, "no\n");
	else fprintf(stdout, "yes\n");
	return 0;
}


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值