牛客周赛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';
}


posted on 2024-07-22 19:37  临江柔  阅读(2)  评论(0编辑  收藏  举报