写这篇博客,主要是贴一段代码。该代码用于解决哈尔滨工业大学(我的母校)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;
}