牛客周赛 Round 21

A.小红的Baidu

思路:字符串大小为 5 5 5,并且 " B a i d u " "Baidu" "Baidu" 每个字符都出现过

void solve()
{
    string s; cin >> s;
    if (s.find('B') != -1 && s.find('a') != -1 && s.find('i') != -1 && s.find('d') != -1 && s.find('u') != -1 && s.size() == 5) {
        puts("Yes");
    }
    else puts("No");
 
}
 

B.小红盖章

思路:模拟实现即可,注意边界。

char str[550][550];
void solve()
{
    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j ++){
            str[i][j] = '.';
        }
    }

   while (q--) {
        int x, y;
        char c[2];
        cin >> x >> y >> *c;
        str[x][y] = c[0];
        for (int i = x; i >= x - 2 && i >= 0; i--) str[i][y] = c[0];
        for (int i = x; i <= x + 2 && i >= 0; i++) str[i][y] = c[0];
        for (int i = y; i >= y - 2 && i >= 0; i--) str[x][i] = c[0];
        for (int i = y; i <= y + 2 && i >= 0; i++) str[x][i] = c[0];
    }


    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << str[i][j];
        }
        cout << endl;
    }
}


C.小红的数组修改

思路:将所有 a a a 累加后求出总和,对每一个数减掉自身对总和的贡献取余,求能让这个总和取余后变为 0 0 0 的方案数即可。


void solve()
{
    int n, p, x;
    cin >> n >> p >> x;
    ll cnt = 0,sum = 0;
    vector<int>a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
    }

    for (int i = 0; i < n; i++) {
        ll ans = (sum - a[i]) % x;
        cnt += (ans + p) / x;//总共能有多少种方案
        if (p >= a[i] && sum % x == 0) cnt--;//能被整除的话原先数是一个方案,因此多了一次。
    }
    cout << cnt << endl;
}

D.括号匹配问题

思路:求连续子串的权值之和。遍历这个字符串,每一个连续子串他的贡献会增加到后面的子串上,因此顺序遍历即可。观察发现,每一个子串的权值之和为 S i + 1 = s i + 2 ∗ s t a c k . t o p ( ) S_{i + 1} = s_i + 2 * stack.top() Si+1=si+2stack.top(),stack放入遍历所得的 ′ ( ′ '(' ( 下标。

void solve()
{
    string s; cin >> s;
    stack<int> S;
    ll res = 0, ans = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(') {
            S.push(i + 1);
        }
        else {
            if (!S.empty()) {
                int v = S.top();
                ans += 2 * v;
                S.pop();

            }
        }
        res += ans;
        //cout << res << ' ' << ans <<endl;
    }
    cout << res << endl;
}
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值