Codeforces Round #785 (Div. 2) A—C

Codeforces Round #785 (Div. 2)

https://codeforces.com/contest/1673

A - Subtle Substring Subtraction

思路

策略:大胆贪心!
只有长度为1的时候,Bob才能赢;
长度为偶数的时候,Alice会把所有的都吃掉;
长度为奇数的时候,Bob只能吃到头尾当中较小的那个。

Code

#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t --) {
        string s;
        cin >> s;
        if (s.size() == 1) {
            cout << "Bob " << s[0] - 'a' + 1 << endl;
            continue;
        }
        cout << "Alice ";
        int n = s.size();
        if (n % 2 == 0) {
            long long ans = 0;
            for (int i = 0; i < n; i ++)
                ans += s[i] - 'a' + 1;
            cout << ans << endl;
        }
        else {
            int minn = min (s[0], s[n-1])- 'a' + 1;
            int maxn = max (s[0], s[n-1])- 'a' + 1;
            long long ans = 0;
            for (int i = 1; i < n - 1; i ++)
                ans += s[i] - 'a' + 1;
            cout << ans + maxn - minn << endl;
        }
    }
    
}

B - A Perfectly Balanced String?

思路

找循环节。
循环节必须是每个字母只出现一次。
具体看代码

Code

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;

int main() {
    int t;
    cin >> t;
    while (t --) {
        string s;
        cin >> s;
        int n = s.size();
        if (n <= 2) {
            cout << "YES" << endl;
            continue;
        }

        set<char>ss;
        for (auto i : s)
            ss.insert (i);
        int len = ss.size();

        bool suc = true;
        for (int i = 0; i < len; i ++)
            for (int j = 0; j < i; j ++)
                if (s[i] == s[j]) //第一段不是循环节,失效
                    suc = false;
        
        for (int i = 0; i < n; i ++)
            if (s[i] != s[i % len]) {
                suc = false;
                break;
            }
        
        if (suc)
            cout << "YES" << endl;
        else
            cout << "NO"  << endl;
    }
    
}

//先统计有多少种字母
//找循环节,看循环节是否重复出现

//t是s的子串,u和v是s中出现过的字母
//u和v在t里出现的次数之差不超过1
//考虑t:长度为1肯定可以
//相同且连续出现两次

C - Palindrome Basis

思路

计数问题
完全背包
注意:要预处理出所有答案先,不然会TLE
(我T了十多次QAQ)

Code

#include <bits/stdc++.h>

using namespace std;
const int mod = 1e9 + 7, N = 4e4;
int n, f[N + 5];

//palindromic
bool check (int x) {
    string s = to_string(x);
    for (int i = 0; i < s.size(); i ++) 
        if (s[i] != s[s.size() - i - 1])
            return false;
    return true;
}

int main() {
    for (int i = 1; i <= N; i ++)
        f[i] = 0;

    vector<int>v;
    for (int i = 1; i <= N; i ++)
        if (check (i))
            v.push_back (i);

    f[0] = 1;
    for (int i = 0; i < v.size(); i ++)  {
        for (int j = v[i]; j <= N; j ++)
            f[j] = (f[j] + f[j - v[i]]) % mod;
    }

    int t;
    cin >> t;
    while (t --) {
        cin >> n;
        
        cout << f[n] << endl;
    }
    
}
//标准计数问题,完全背包
//还不能是回文
posted @ 2022-05-01 17:47  Sakana~  阅读(30)  评论(0编辑  收藏  举报