Codeforces Round #754 (Div. 2) C. Dominant Character(Codeforces Round #754 (Div. 2) C. Dominant Character)

题目:Problem – C – Codeforces

如代码,一共有七种情况,注意不要漏掉  “accabba”  , “abbacca”  两种情况;

使用find()函数可简化代码,使用方法如下

代码:

#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll T;
    cin >> T;
    while (T--)
    {
        ll n, ans = -1;
        string s;
        cin >> n >> s;
        if (s.find("accabba") != s.npos)
            ans = 7;
        if (s.find("abbacca") != s.npos)
            ans = 7;
        if (s.find("acba") != s.npos)
            ans = 4;
        if (s.find("abca") != s.npos)
            ans = 4;
        if (s.find("aba") != s.npos)
            ans = 3;
        if (s.find("aca") != s.npos)
            ans = 3;
        if (s.find("aa") != s.npos)
            ans = 2;
        cout << ans << endl;
    }
    return 0;
}
————————

题目:Problem – C – Codeforces

Such as code, there are seven cases in total. Be careful not to miss them    “accabba”   ,  “abbacca”   Two cases;

Use the find () function to simplify the code as follows

code:

#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll T;
    cin >> T;
    while (T--)
    {
        ll n, ans = -1;
        string s;
        cin >> n >> s;
        if (s.find("accabba") != s.npos)
            ans = 7;
        if (s.find("abbacca") != s.npos)
            ans = 7;
        if (s.find("acba") != s.npos)
            ans = 4;
        if (s.find("abca") != s.npos)
            ans = 4;
        if (s.find("aba") != s.npos)
            ans = 3;
        if (s.find("aca") != s.npos)
            ans = 3;
        if (s.find("aa") != s.npos)
            ans = 2;
        cout << ans << endl;
    }
    return 0;
}