POJ-3746 Teacher YYF(模擬)

題目鏈接

題意

簡單講就是要根據給定單詞和詞性以及語法規則寫一個程序判斷各種句子語法的合法性

思路

看了別人的思路,最好的方法是根據英語語法將結構分爲主謂賓介,之後再細分其中的詞性組合,據此判斷合法性。

這方法,

代碼

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章