1 条题解

  • 0
    @ 2022-10-13 9:42:42

    经典的字符 'A'~'Z' 映射到 0~25 后计数。

    #include <bits/stdc++.h>
    using namespace std;
    int t, n, ans;
    string s;
    int cnt[30];
    int main()
    {
        cin >> t;
        while (t--)
        {
            cin >> n;
            cin >> s;
            //多组数据清空数组和变量
            for (int i = 0; i < 26; i++)
                cnt[i] = 0;
            ans = 0;
            //计算答案
            for (int i = 0; i < n; i++)
            {
                int now = s[i] - 'A';
                cnt[now]++;
                if (cnt[now] == 1)
                    ans += 2;
                else
                    ans += 1;
            }
            cout << ans << "\n";
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    1097
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    12
    已通过
    8
    上传者