笔试题------英语句子结构

句子结构合法:

主语有1到多个

谓语有且仅有1个

宾语有0到多个

#include <bits/stdc++.h>
using namespace std;

// 三个单词表
unordered_set<string> zhuyu;
unordered_set<string> weiyu;
unordered_set<string> binyu;

bool judge(vector<string> &temp){
    // return true;
    int i = 0, len = temp.size();
    if(len < 2){
        // cout<<"长度小于2"<<endl;
        return false; // 至少有主谓
    } 
    // 判断主语
    for(; i < len; ++i){
        if(i == 0 && zhuyu.find(temp[i]) == zhuyu.end()){ // 验证主语存在
            // cout<<"主语不存在"<<endl;
            return false;
        }
        if(zhuyu.find(temp[i]) != zhuyu.end()){ // 主语可有多个
            continue;
        }
        break; // 跳到谓语判断
    }
    //判断谓语
    for(; i < len; ++i){
        if(weiyu.find(temp[i]) == weiyu.end()){ // 谓语不存在
            // cout<<"谓语不存在"<<endl;
            return false;
        }; 
        i++;
        break; // 谓语有一个
    }
    // 判断宾语
    for(; i < len; ++i){
        if(weiyu.find(temp[i]) != weiyu.end()){
            // cout<<"谓语有多个"<<endl;
            return false;
        }else continue;
    }
    return true;
}

void split_string(const string str, vector<string>&vec, const std::string cut) {
    std::string::size_type begin_index, end_index;
    end_index = str.find(cut);
    begin_index = 0;
    while(std::string::npos != end_index)
    {
        vec.push_back(str.substr(begin_index, end_index-begin_index));
        begin_index = end_index+ cut.size();
        end_index = str.find(cut, begin_index);
    }
    if(begin_index != str.length()){
        vec.push_back(str.substr(begin_index));
    }
}


int main(){
    int n1, n2, n3;
    cin>>n1>>n2>>n3;

    for(int i = 0; i < n1; ++i){
        string t;
        cin>>t;
        zhuyu.insert(t);
    }

    for(int i = 0; i < n2; ++i){
        string t;
        cin>>t;
        weiyu.insert(t);
    }

    for(int i = 0; i < n3; ++i){
        string t;
        cin>>t;
        binyu.insert(t);
    }

    int m;
    cin>>m;
    getchar();

    for(int i = 0; i < m; ++i){
        vector<string> temp;
        string t;
        getline(cin, t);
        split_string(t, temp, " ");
        if(judge(temp)) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        temp.clear();
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值