C. Beautiful Sequence + D. Palindrome Shuffle【Educational Codeforces Round 174 (Rated for Div. 2)】

C. Beautiful Sequence

在这里插入图片描述

思路:

只有123,那就很简单了,一定是1开头3结尾,中间有 n n n 个2,子序列数量就是 2 n − 1 2^n-1 2n1

直接暴力统计明显不行,正确的做法是动态规划。(赛时直接推了数学式子然后写了个模逆元,最后fst死在负数模上了,吃一堑长一智)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define int long long
#define pb push_back
#define pii pair<int,int>
#define FU(i, a, b) for(int i = (a); i <= (b); ++ i)
#define FD(i, a, b) for(int i = (a); i >= (b); -- i)
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;

int n,a[200005],pre[200005],cnt[200005];

void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        if(a[i] == 1) {
            pre[i] = pre[i - 1] + 1;
            cnt[i] = cnt[i - 1] + 1;
            pre[i] %= MOD;
        }
        else if(a[i] == 2) {
            pre[i] = pre[i - 1] * 2;
            cnt[i] = cnt[i - 1];
            pre[i] %= MOD;
        }
        else {
            pre[i] = pre[i - 1];
            cnt[i] = cnt[i - 1];
            int x = (pre[i] - cnt[i] + MOD) % MOD;
            ans = (ans + x) % MOD;;
        }
    }
    cout << ans << endl;
}

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

D. Palindrome Shuffle

在这里插入图片描述

思路:

贪心即可,也是很简单的题了(可惜赛后秒过)
先对半划分成左右两侧,数左右两侧每种字符数量是否相同,
如果相同:只用在一边操作即可,找左右两侧相对应的最短不同子串。
如果不同:说明一定会跨过中间操作,从左往右和从右往左各尝试一次即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define FU(i, a, b) for (int i = (a); i <= (b); ++i)
#define FD(i, a, b) for (int i = (a); i >= (b); --i)
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;

void solve() {
    string s;
    cin >> s;
    int n = s.length();
    int l = 0, r = n - 1;
    while (l < r && s[l] == s[r]) {
        l++;
        r--;
    }
    if (l >= r) {
        cout << 0 << endl;
        return;
    }
    // cout<<"l,r="<<l<<" "<<r<<endl;
    string sub = s.substr(l, r - l + 1);
    int m = sub.length();
    string s1 = sub.substr(0, m / 2);
    string s2 = sub.substr(m / 2);
    reverse(s2.begin(), s2.end());
    // cout<<s1<<" "<<s2<<endl;
    vector<int> cnt1(26, 0), cnt2(26, 0);
    vector<int> total(26, 0);

    for (char c : s1) {
        cnt1[c - 'a']++;
        total[c - 'a']++;
    }
    for (char c : s2) {
        cnt2[c - 'a']++;
        total[c - 'a']++;
    }
    bool flag = true;
    for (int i = 0; i < 26; i++) {
        if (cnt1[i] != cnt2[i]) {
            flag = false;
            break;
        }
    }
    // cout<<"m= "<<m<<endl;
    if (flag) { // 两边相同
        int i = 0, j = m / 2 - 1;
        while (i < s1.length() && s1[i] == s2[i]) {
            i++;
        }
        while (j >= 0 && s1[j] == s2[j]) {
            j--;
        }
        // cout<<"i,j = "<<i<<" "<<j<<endl;
        cout << j - i + 1 << endl;
    } else { // 两边不同

        int ans1 = 0, ans2 = 0;
        vector<int> cnt(26, 0);
        // 从左往右遍历
        for (int i = 0; i < m; i++) {
            cnt[sub[i] - 'a']++;
            if (cnt[sub[i] - 'a'] > total[sub[i] - 'a'] / 2) {
                ans1 = m - i;
                break;
            }
        }
        cnt = vector<int>(26, 0);
        // 从右往左遍历
        for (int i = m - 1; i >= 0; i--) {
            cnt[sub[i] - 'a']++;
            if (cnt[sub[i] - 'a'] > total[sub[i] - 'a'] / 2) {
                ans2 = i + 1;
                break;
            }
        }
        // cout<<ans1<<" | "<<ans2<<endl;
        cout << min(ans1, ans2) << endl;
    }
}

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

又掉大分,惨痛www

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值