第29次CCF计算机软件能力认证 LDAP题解

题目描述

给一个类似于前缀逻辑表达式的 B N F BNF BNF 范式,求符合条件的结果

题目链接:https://www.acwing.com/problem/content/5021/

样例

输入样例:

2
1 2 1 2 2 3
2 2 2 3 3 1
4
1:2
3~1
&(1:2)(2:3)
|(1:2)(3:1)

输出样例:

1

1
1 2

算法

代码中采用了类似于后缀表达式求值的方法 https://www.acwing.com/problem/content/3305/
考虑用栈记录当前遇到的符号和目前合法的状态,模拟部分和后缀表达式基本一致,其中遇到 : : :或者~的时候处理的方法就是把所有合法的 i d id id 全部记录下来,这里采用 b i t s e t bitset bitset 进行压位优化。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;
#define x first
#define y second
void solve() {
    int n;
    cin >> n;
    vector<int> id(n + 1);
    map<PII, vector<int>> mp; 
    map<int, vector<int>> mpp;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        int dn, num;
        cin >> dn >> num;
        id[i] = dn;
        for (int j = 1; j <= num; j++) {
            int key, val;
            cin >> key >> val;
            mp[{key, val}].emplace_back(i);
            mpp[key].emplace_back(i);
        }
    }
    int m;
    cin >> m;
    while(m --) {
        stack<int> ops;
        stack<bitset<2501>> nums;
        string s;
        cin >> s;
        auto eval = [&]() {
            bitset<2501> b1 = nums.top(); nums.pop();
            bitset<2501> b2 = nums.top(); nums.pop();
            char ch = ops.top(); ops.pop();
            if(ch == '&') {
                nums.push(b1 & b2);
            } else {
                nums.push(b1 | b2);
            }
        };
        for (int i = 0; i < s.size(); i++) {
            if(s[i] == '|' || s[i] == '&') {
                ops.push(s[i]);
            } else if(isdigit(s[i])) {
                int key = 0;
                while(i < s.size() && isdigit(s[i])) {
                    key = key * 10 + s[i] - '0';
                    i++;
                }
                int op = 0;
                if(s[i] == '~') op = 1;
                int val = 0;
                i++;
                while(i < s.size() && isdigit(s[i])) {
                    val = val * 10 + s[i] - '0';
                    i++;
                }
                bitset<2501> b;
                b.reset();
                for (auto t: mp[{key, val}]) {
                    b[t] = 1;
                }
                if(op == 1) {
                    for (auto t: mpp[key]) {
                        b.flip(t);
                    }  
                } 
                nums.push(b);
            } else if(s[i] == '(' && !isdigit(s[i + 1])) {
                ops.push(s[i]);
            } else if(s[i] == ')'){
                while(ops.top() != '(') eval();
                ops.pop();
            }
        }
        while(ops.size()) eval();
        // assert(nums.size() == 1);
        bitset<2501> b = nums.top();
        vector<int> v;  
        for (int i = 1; i <= n; i++) {
            if(b[i] == 1) {
                v.emplace_back(id[i]);
            }
        }
        sort(v.begin(), v.end());
        for (int i = 0; i < v.size(); i++) {
            cout << v[i] << " ";
        }
        cout << "\n";
    }
    
}
int main() {
   ios::sync_with_stdio(false);
   cin.tie(0), cout.tie(0);
   //cout << fixed << setprecision(10);
   //init();
   
   int T = 1;
   //cin >> T;
   while(T --) solve();
   
   return 0;
}
从提供的引用中可知,涉及第37CCF计算机软件能力认证的两个题目相关代码。 ### 题目一:机器人饲养指南 该题使用动态规划解决。将苹果的个数看作物品质量(共有m种苹果,不考虑哪天吃几个,只关注组合而非排列),苹果个数的幸运值当作物品价值,n个苹果代表背包的容积。通过一个二维的`dp`数组来表示装前1 - i种物品、背包容量为j时的最大价值。代码实现如下: ```cpp #include<bits/stdc++.h> using namespace std; //动态规划 int n, m; vector<int> value(103, 0); int main() { cin >> n >> m; for (int i = 1; i <= m; i++) cin >> value[i]; //苹果的个数代表物品质量(共有m种苹果)(并不在乎哪天吃几个 只在乎组合不在乎排列) //苹果的个数的幸运值代表物品的价值 //n个苹果代表背包的容积 //dp数组代表装前1-i种物品 背包容量为j的最大价值 vector <vector<int>> dp(m+2, vector<int>(10004)); for (int j = 1; j <= n; j++) dp[1][j] = dp[1][j - 1] + value[1]; for (int i = 2; i <= m; i++) { //遍历物品 for (int j = 1; j <= n; j++) { //遍历背包容量 if (j < i) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - i] + value[i]); } } cout << dp[m][n]; return 0; } ``` ### 题目二:集体锻炼 该题强调能用`long`类型就用`long`,能两数取模就不三数之后再取模。代码中定义了求最大公约数的函数`ggcd`,通过`gcd_some`函数计算区间`[l, r]`内元素的最大公约数并取模。主函数中通过两层循环遍历所有可能的区间`[l, r]`,根据`l`和`r`是否相等计算结果并取模。代码实现如下: ```cpp #include<bits/stdc++.h> using namespace std; int n; const long long mod = 998244353; vector<long long> a(1003, 0); long long ggcd(long long a, long long b) { if (b > a) { long long temp = a; a = b; b = temp; } while (b) { long long temp = a % b; a = b; b = temp; } return a; } long long gcd_some(int l,int r) { long long res = a[l]; for (int i = l+1; i <= r; i++) { res = ggcd(res, a[i]); } return res % mod; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } long long res = 0; for (int l = 1; l <= n; l++) { for (int r = l; r <= n; r++) { if (l == r) { res += ((a[l]*l)%mod*r)%mod; } else res += (gcd_some(l, r)*l*r)%mod; res = res % mod; } } cout << res%mod; return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值