HNU_10076

    这个和POJ的3332十分类似,都是判断一个字符串是否合法,具体怎么想可以参考一下我的另一篇题解,http://www.cnblogs.com/staginner/archive/2012/01/24/2329131.html,代码为了看起来思路清晰一些,所以写得就十分啰嗦了,很多地方是可以简化的。

#include<stdio.h>
#include<string.h>
#define MAXD 10010
char noun[][10] = {"tom", "jerry", "goofy", "mickey", "jimmy", "dog", "cat", "mouse"};
char verb[][10] = {"hate", "love", "know", "like", "hates", "loves", "knows", "likes"};
char b[MAXD], word[MAXD];
int pat;
void delword(int &i)
{
while(b[i] && b[i] != ' ')
++ i;
}
void delblank(int &i)
{
while(b[i] == ' ')
++ i;
}
int find_article()
{
if(strcmp(word, "a") == 0 || strcmp(word, "the") == 0)
return 1;
return 0;
}
int find_noun()
{
int i;
for(i = 0; i < 8; i ++)
if(strcmp(noun[i], word) == 0)
return 1;
return 0;
}
int find_verb()
{
int i;
for(i = 0; i < 8; i ++)
if(strcmp(verb[i], word) == 0)
return 1;
return 0;
}
void pattern()
{
if(pat == 0)
{
if(find_article())
pat = 1;
else
pat = 2;
}
else if(pat == 1)
{
pat = 2;
}
else if(pat == 2)
{
if(strcmp(word, "and") == 0)
pat = 3;
else
pat = 4;
}
else if(pat == 3)
{
if(find_article())
pat = 1;
else
pat = 2;
}
else if(pat == 4)
{
if(find_article())
pat = 5;
else
pat = 6;
}
else if(pat == 5)
{
pat = 6;
}
else if(pat == 6)
{
if(strcmp(word, "and") == 0)
pat = 7;
else
pat = 0;
}
else if(pat == 7)
{
if(find_article())
pat = 5;
else
pat = 6;
}
}
int check()
{
if(pat == 0)
{
if(strcmp(word, ",") == 0)
return 1;
else
return 0;
}
else if(pat == 1)
{
if(find_article())
return 1;
else
return 0;
}
else if(pat == 2)
{
if(find_noun())
return 1;
else
return 0;
}
else if(pat == 3)
{
if(strcmp(word, "and") == 0)
return 1;
else
return 0;
}
else if(pat == 4)
{
if(find_verb())
return 1;
else
return 0;
}
else if(pat == 5)
{
if(find_article())
return 1;
else
return 0;
}
else if(pat == 6)
{
if(find_noun())
return 1;
else
return 0;
}
else if(pat == 7)
{
if(strcmp(word, "and") == 0)
return 1;
else
return 0;
}
return 0;
}
int solve()
{
int i, j, k;
gets(b);
pat = 0;
for(i = 0, delblank(i); b[i];)
{
sscanf(b + i, "%s", word);
pattern();
if(!check())
return 0;
delword(i);
delblank(i);
}
if(pat == 6)
return 1;
else
return 0;
}
int main()
{
int t;
gets(b);
sscanf(b, "%d", &t);
while(t --)
{
if(solve())
printf("YES I WILL\n");
else
printf("NO I WON'T\n");
}
return 0;
}

 

posted on 2012-01-24 13:52  Staginner  阅读(186)  评论(0编辑  收藏  举报