hdoj.1433 Simply Syntax【递归调用】 2015/04/24

Simply Syntax

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 314    Accepted Submission(s): 163


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
 

Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[300];
int len;
bool flag;
bool OK( int x,int y ){
    if( x == y ){
        if( s[x] >= 'p' && s[x] <= 'z' )
            return true;
        else return false;
    }
    if( s[x] == 'N' ){
        if( OK(x+1,y) )
            return true;
        else return false;
    }
    if( s[x] == 'C' || s[x] == 'D' || s[x] == 'E' || s[x] == 'I' ){
        for( int i = x+1 ; i <= y ; ++i )
            if( OK(x+1,i) && OK(i+1,y) )
                return true;
        return false;
    }
    return false;
}
int main(){
    while(scanf("%s",s)!=EOF){
        flag = false;
        len = strlen(s);
        if( len == 1 && ( s[0] >= 'p' && s[0] <= 'z' ) )
            flag = true;
        else{
            if( OK(0,len-1) )
                flag = true;
        }
        if( flag )
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
注:算是比较经典的递归吧,递归思想就是将整个字符串拆分,一直拆分,这样一层一层地拆分最后就将这个字符串转化成若干个字符了,判断字符是否符合,就OK了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值