Teacher YYF(POJ-3746)

题面

As we all know, grammar is very important when learning English. Now, YYF become a teacher of a primary school. The students are good at memory so they can tell the meaning and the function (noun, verb …) of the words in the textbook. But, they cannot use these words properly.

In YYF’s mind, writing sentences is a good way to learn grammar. So he tells the student to write 20 sentences a day, using the word learned in the class. As YYF has a lot of student, he will receive many sentences from his student. What a horrible work to check all the sentences. You are one of YYF’s friends, so he asks you for help. You task is to write a program to check the sentences.

To make the work simple, YYF chooses a part of the grammar: All the words can be grouped into seven divisions (noun, pronoun, adjective, adverb, preposition, article, and verb). A verb can be transitive or intransitive. So we use “n.”, “pron.”, “adj.”, “adv.”, “prep.”, “art.”, “vt.” and “vi.” to be short of noun, pronoun, adjective, adverb, preposition, article, transitive verb and intransitive verb. If a word is marked as “v.”, it can be used as either transitive verb or intransitive verb.

Here comes the sentence structure:

  1. Subject + Intransitive Verb
  2. Subject + Transitive Verb + Object

Noun and pronoun can be used as Subject or Object. When using a noun, an article should be placed ahead of it. A noun can be modified by an adjective and a verb can be modified by an adverb. When an adjective is used to modify a noun, it should be put between article and noun. When an adverb is used to modify a verb, it should be put ahead of the verb. A prepositional phrase can be put ahead of Subject, between Subject and Verb, behind Intransitive Verb, between Verb and Object, or behind Object. A prepositional phrase is made up of a preposition and a noun/pronoun. In one sentence, at most one prepositional phrase is allowed. Any two parts of the sentence cannot intersect. For example, “He is a good student” is OK, but “He a good is student” is not. Every word in the dictionary will have only one function. The words are not case sensitive and Subject-Verb Agreement does not matter. That’s all the rules. Now, it’s your time to show.

输入

The input contains only one case. The first line specifies two number N and M (1 ≤ N, M ≤ 5000). The next N lines will be the words and the functions. Every line contains a word and its function, separated by a space. The next M lines will be the sentences – one sentence per line. Each sentences contains at most 20 words. Every word in the sentences will appear in the dictionary.

输出

The output contains M lines. For each line, output “YES” if the sentence is OK, and output “NO” if not.

样例输入

 110 6
 2he pron.
 3see vt.
 4a art.
 5baby n.
 6at prep.
 7the art.
 8airport n.
 9happy adj.
10guess v.
11immediately adv.
12He guess.
13He see baby.
14Happy he see a baby.
15He immediately see a baby.
16He see a baby immediately.
17At the airport, he see a happy baby.

样例输出

1YES
2NO
3NO
4YES
5NO
6YES

提示

Please read the Problem Description carefully. Do not use your own English knowledge to construct rules.

思路

不太符合专题内容,估计是题目拉错没删,【题解】HDU-3746 Cyclic Nacklace。网上找的用stl的实现,很灵活。

代码

 1map<string,string> mp;
 2map<string,bool> ste;
 3map<string,char> str, fun;
 4
 5void init()
 6{
 7    fun["n."]='0';
 8    fun["pron."]='1';
 9    fun["adj."]='2';
10    fun["adv."]='3';
11    fun["prep."]='4';
12    fun["art."]='5';
13    fun["vt."]='6';
14    fun["vi."]='7';
15    fun["v."]='8';
16
17    str["450"]='A'; // 介词短语
18    str["4520"]='A';
19    str["41"]='A';
20    str["1"]='S';   // 主/宾语
21    str["50"]='S';
22    str["520"]='S';
23    str["7"]='I';   // 不及物谓语
24    str["37"]='I';
25    str["6"]='T';   // 及物谓语
26    str["36"]='T';
27    str["8"]='V';   // 通用谓语
28    str["38"]='V';
29    // 句子可能的总体结构
30    ste["SI"]=1;
31    ste["STS"]=1;
32    ste["SV"]=1;
33    ste["SVS"]=1;
34    ste["ASI"]=1;
35    ste["ASTS"]=1;
36    ste["ASV"]=1;
37    ste["ASVS"]=1;
38    ste["SAI"]=1;
39    ste["SATS"]=1;
40    ste["SAV"]=1;
41    ste["SAVS"]=1;
42    ste["SIA"]=1;
43    ste["STAS"]=1;
44    ste["SVA"]=1;
45    ste["SVAS"]=1;
46    ste["STSA"]=1;
47    ste["SVSA"]=1;
48}
49
50bool check(string s){
51    string res="", c="";
52    for(int i=0; i<s.size(); i++){
53        c += s[i];
54        if(str[c] > 0){
55            res += str[c];
56            c = "";
57        }
58    }
59    res += c;
60    return ste[res];
61}
62
63string work(string s){
64    stringstream ss(s);
65    string st, ans;
66    while(ss >> st){
67        int len=st.size(), flag=0;
68        st[0] = tolower(st[0]);
69        if(st[len-1] == '.'){
70            flag = 1;
71            st.erase(--st.end());
72        }
73        if(st[len-1] == ',')
74            st.erase(--st.end());
75        ans += fun[mp[st]];
76        if(flag && !check(ans))
77            return "NO";
78    }
79    return "YES";
80}
81
82int main()
83{
84    init(); int n, m;
85    while(cin >> n >> m)
86    {
87        mp.clear();
88        string s, s1, s2;
89        for(int i=0; i<n; i++){
90            cin >> s1 >> s2;
91            mp[s1] = s2;
92        }
93        cin.get();
94        for(int i=0; i<m; i++){
95            getline(cin, s);
96            cout << work(s) << endl;
97        }
98    }
99}