POJ-3746 Teacher YYF(模拟)

本文介绍了一种基于英语语法规则的句子合法性检查程序。通过将句子结构分解为主谓宾介,并利用词性标记和预定义的语法规则,该程序能够判断输入句子是否符合英语语法规范。

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

题目链接

题意

简单讲就是要根据给定单词和词性以及语法规则写一个程序判断各种句子语法的合法性

思路

看了别人的思路,最好的方法是根据英语语法将结构分为主谓宾介,之后再细分其中的词性组合,据此判断合法性。

这方法,

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
using namespace std;
map<string,int> fun; //把每种词标号 
map<string,int> num; //将题目中给出的词标号 
map<string,char> str; //记录每种主语宾语谓语介词短语的每种形式,打表 
map<string,int> ste; //每种句子形式,打表 
void init()
{
	fun["n."]=0;
	fun["pron."]=1;
	fun["adj."]=2;
	fun["adv."]=3;
	fun["prep."]=4;
	fun["art."]=5;
	fun["vt."]=6;
	fun["vi."]=7;
	fun["v."]=8;
	
	str["450"]='A'; //介词短语 
	str["4520"]='A';
	str["41"]='A';
	str["1"]='S'; //主/宾语 
	str["50"]='S';
	str["520"]='S';
	str["7"]='I'; //不及物谓语 
	str["37"]='I';
	str["6"]='T'; //及物谓语 
	str["36"]='T';
	str["8"]='V'; //通用谓语 
	str["38"]='V';
	//句子可能的总体结构 
	ste["SI"]=1;
	ste["STS"]=1;
	ste["SV"]=1;
	ste["SVS"]=1;
	ste["ASI"]=1;
	ste["ASTS"]=1;
	ste["ASV"]=1;
	ste["ASVS"]=1;
	ste["SAI"]=1;
	ste["SATS"]=1;
	ste["SAV"]=1;
	ste["SAVS"]=1;
	ste["SIA"]=1;
	ste["STAS"]=1;
	ste["SVA"]=1;
	ste["SVAS"]=1;
	ste["STSA"]=1;
	ste["SVSA"]=1;
}
int n,m;
int main()
{
	string a,b;
	init();
	scanf("%d%d",&n,&m);
	while(n--)
	{
		cin>>a>>b;
		num[a]=fun[b];
	}
	while(m--)
	{
		bool flag=false;
		b="";
		while(cin>>a) //转化为词性表示 
		{
			if(isupper(a[0])) a[0]=a[0]+32;
			int len=a.length();
			if(a[len-1]=='.')
			{
				flag=true;
				a.erase(len-1,1);
			}
			if(a[len-1]==',') a.erase(len-1,1);
			b+='0'+num[a];
			if(flag) break;
		}
		//cout<<b<<endl;
		string c,d;
		c=d="";
		for(int i=0;i<b.length();i++) //转化为结构表示 
		{
			c+=b[i];
			if(str[c])
			{
				d+=str[c];
				c="";
			}
		}
		d+=c;
		//cout<<d<<endl;
		if(ste[d]) cout<<"YES"<<endl; //判断是否符合 
		else cout<<"NO"<<endl;
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值