牛客周赛52总结

小红的最大字典序

思路:使用优先队列进行多路归并

#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define endl '\n'
#define pq priority_queue
using namespace std;
typedef pair<int,int> pii;

void solve(){
	priority_queue<pair<char,string>>q;
	int n;
	for(int i = 0;i < n;i ++){
		string s;
		cin >> s;
		q.push({s[0], s});
	}
	string ans = "";
	while(q.size()){
		auto [c, s] = q.top();
		q.pop();
		s.erase(0, 1);
		ans += c;
		if(s.size() > 0)
			q.push({s[0], s});
	}
	cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t = 1;
    while(t--)
    {
        solve();
    }
    return 0;
}
  

思路:这里有个结论,只要左右括号相等,通过循环移位一定可以成为合法的字符串序列,所以这道题就变成一道组合数学的题了

int fac[N], inf[N];

int ksm(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int C(int n, int m) {
    return fac[n] * inf[n - m] % mod * inf[m] % mod;
}


void solve() {
    fac[0] = inf[0] = 1;
    for (int i = 1; i < N; i++) {
        fac[i] = fac[i - 1] * i % mod;
        inf[i] = inf[i - 1] * ksm(i, mod - 2) % mod;
    }
    int n; cin >> n;
    string s; cin >> s;
    int c1 = 0, c2 = 0, cv = 0;
    for (auto i : s) {
        c1 += (i == '(');
        c2 += (i == ')');
        cv += (i == '?');
    }
    if (c1 > n / 2 || c2 > n / 2 || n & 1) {
        cout << 0 << '\n';
    } else cout << C(cv, abs(c1 - c2) + (cv - abs(c1 - c2)) / 2) << '\n';
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临江浪怀柔ℳ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值