acm pku 1126 Simple Syntax的具体实现
来源:互联网 发布:新西兰网络商 编辑:程序博客网 时间:2024/06/05 11:45
Simply Syntax
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
East Central North America 1994
这道题目可以通过判断“0rule~4rule”来得到解决。假设用bResult[i][j]表示自第i个元素起到其后面的j个元素所组成的词是否是合法的(0≤i<输入单词长度,0<j<输入字符长度) ,若是则bResult[i][j]=true;否则,bResult[i][j]=false。这样,bResult[0][输入字符长度] 就是要求的结果,如果为真则输出YES,否则输出NO。关于bResult[i][j],可以按如下公式进行计算:
bResult[i][j] = True if (words[i]=p~z, i=0, j = 1;) True if (words[i]=N, bResult[i+1][j-1]=true; j>1;) True if (words[i]=C,D,E,I, bResult[i+1][j-1]=true; j>1; 存在bResult[i+1][k]=bResult[i+k][j-1-k]=true, 0<k<j) false otherwise
具体实现:
//20100530
#include "iostream"
using namespace std;
const int N = 256;
char cinput[N];
bool bResult[N][N];
void Judge(int begin, int num)
{
int i, j, k;
if(num == 1)
{
if(cinput[begin] >= 'p' && cinput[begin] <= 'z') bResult[begin][1] = true;
return;
}
for(i = begin; i < begin+num; i++)
{
if(cinput[i] >= 'p' && cinput[i] <= 'z') bResult[i][1] = true;
}
for(i = begin+num-1 -1; i > -1; i--)
{
for(j = 2; j <= begin+num-i; j++)
{
// if(cinput[i] >= 'p' && cinput[i] <= 'z' && bResult[i+1][j-1]) bResult[i][j] = true;
if(cinput[i] == 'N' && bResult[i+1][j-1]) bResult[i][j] = true;
else if(cinput[i] == 'C' || cinput[i] == 'D' || cinput[i] == 'E' || cinput[i] == 'I')
{
for(k = 1; k < j; k++)
{
if(bResult[i+1][k] && bResult[i+k+1][j-1-k]) {bResult[i][j] = true;break;}
}
}
}
}
}
int main(void)
{
int i, len;
while(cin >> cinput)
{
len = strlen(cinput);
for(i = 0; i <= len; i++) memset(bResult[i], false, sizeof(bool)*N);
Judge(0, len);
if(bResult[0][len]) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
执行结果:
Problem: 1126
User: uestcshe
Memory: 164K
Time: 0MS
Language: C++
Result: Accepted
- acm pku 1126 Simple Syntax的具体实现
- acm pku 1132 Border的具体实现
- acm pku 1107 W's Cipher的具体实现方法
- acm pku 1118 Lining Up的具体实现方法
- POJ 1126 Simple Syntax
- acm pku 1068 Parencodings的实现
- acm pku 1056 IMMEDIATE DECODABILITY的代码实现
- acm pku 1125 Stockbroker Grapevine的Dijkstra算法实现
- acm pku 1142 Smith Numberd的模拟实现方法
- acm pku 1226 Substring的算法分析与实现
- acm pku 1247 Magnificent Meatballs的算法实现
- acm pku 1251 Jungle Roads的算法分析与实现
- acm pku 1258 Agri-Net的Prim算法实现
- acm pku 1260 Pearls的动态规划算法实现
- acm pku 1298 The hardest problem ever的实现
- acm pku 1230Pass-Muraille的贪婪算法实现
- acm pku 1299 Polar Exploer的简单实现
- PKU ACM 1002 487-3279 C++实现
- 请问你的梦想是什么?
- poj 1054 讨厌的青蛙
- 博客地图
- C 语言高手请帮忙讲解一下怎么调用函数
- Active Directory 到 IBM Lotus 的迁移与集成
- acm pku 1126 Simple Syntax的具体实现
- 直方图均衡化
- j2me游戏步长算法
- 正则表达式
- android站点的镜像
- linux命令之zip压缩与unzip解压缩命令
- js-- prototype属性
- linux开机启动服务详解
- netbeans6.8中的字体配置-NetBeans