Codeforces Round 979 B Treasure Hunt

原题链接
这道题的题意是这样的:三个人有三个字符串,一共N个回合,每个回合必须将其中一个字母变成不同的字母,问N轮之后,谁的字符串中相同的子串个数最多。
解法很SB的,先贪心的找到原来的串里最多的那个字母,然后我们肯定用这个字母。那么最后的答案就是字符串的长度和这个最大值加上回合数的最小值。
但是,如果一个串找到的最大值等于这个串的长度时,我们就要考虑一下。如果我们要保存这个串的最大值,可以让其中一个字符变动N次,反正最后一次变回来就行。但是N等于一的时候,这个方法就失效了,于是答案就是最大值-1.这个地方没想明白WA了好几次……

#include <bits/stdc++.h>
#include <cstring>
#define ll long long
#define fi first
#define se second
using namespace std;
int main()
{
    map<char,int> ss;
    int ans[3];
    string s[3];
    int n;
    cin>>n>>s[0]>>s[1]>>s[2];
    int m = s[0].size();
    for(int i = 0; i<3; i++)
    {
        ss.clear();
        ans[i] = 0;
        for(auto c : s[i]) ss[c] ++;
        int M = 0;
        for(auto p : ss)
        {
            M = max(M,p.se);
        }
        if(s[i].size() == 1) ans[i] == 1;
        else
        {
            if(M == s[i].size())
            {
                if(n ==1) ans[i] = M -1;
                else ans[i] = M;
            }
            else ans[i] = min((int)s[i].size(),M + n);
        }
       // cout<<ans[i]<<endl;
    }

    int mx = max({ans[0],ans[1],ans[2]});
    int cnt = 0;
    for(int i = 0;i<3;i++) if(ans[i] == mx) cnt++;
    if(cnt >= 2) cout<<"Draw"<<endl;
    else if(ans[0] == mx) cout<<"Kuro"<<endl;
    else if(ans[1] == mx) cout<<"Shiro"<<endl;
    else if(ans[2] == mx) cout<<"Katie"<<endl;
    return  0;
}

猜你喜欢

转载自blog.csdn.net/lingzidong/article/details/80325365
今日推荐