首页 > 其他分享 >ABC301

ABC301

时间:2023-06-09 21:57:53浏览次数:38  
标签:26 int rep ABC301 ++ cs ct

T1:Overall Winner

模拟

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

int main() {
    int n;
    string s;
    cin >> n >> s;
    
    int t = 0, a = 0;
    rep(i, n) {
        if (s[i] == 'T') t++;
        else a++;
    }
    
    if (t != a) {
        if (t > a) puts("T");
        else puts("A");
    }
    else { // t == a
        if (s.back() == 'A') puts("T");
        else puts("A");
    }
    
    return 0;
}

T2:Fill the Gaps

模拟

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    
    vector<int> ans;
    ans.push_back(a[0]);
    for (int i = 1; i < n; ++i) {
        if (a[i-1] < a[i]) {
            for (int x = a[i-1]+1; x <= a[i]; ++x) ans.push_back(x);
        }
        else {
            for (int x = a[i-1]-1; x >= a[i]; --x) ans.push_back(x);
        }
    }
    
    rep(i, ans.size()) cout << ans[i] << ' ';
    
    return 0;
}

T3:AtCoder Cards

只需要检查数量的变化,而不是实际替换 ‘@’

先统计每种字符的数量,比如如果 'a' 的个数不同,就将 '@' 替换成 'a' 以减少不足的数量,然后如果最后每个字符的数量都相同的话就 OK

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

vector<int> count(string s) {
    vector<int> res(27);
    for (char c : s) {
        if (c == '@') res[26]++;
        else res[c-'a']++;
    }
    return res;
}

bool solve() {
    string s, t;
    cin >> s >> t;
    
    vector<int> cs = count(s);
    vector<int> ct = count(t);
    
    string A = "atcoder";
    vector<bool> inA(26);
    for (char c : A) inA[c-'a'] = true;
    
    rep(i, 26) if (!inA[i]) {
        if (cs[i] != ct[i]) return false;
    }
    
    rep(i, 26) if (inA[i]) {
        if (cs[i] < ct[i]) {
            cs[26] -= ct[i]-cs[i];
        }
        else {
            ct[26] -= cs[i]-ct[i];
        }
    }
    
    if (cs[26] < 0 or ct[26] < 0) return false;
    return true;
}

int main() {
    if (solve()) puts("Yes");
    else puts("No");
    
    return 0;
}

标签:26,int,rep,ABC301,++,cs,ct
From: https://www.cnblogs.com/Melville/p/17470307.html

相关文章