1 条题解

  • 1
    @ 2024-11-12 21:41:36
    /**
     *    author: 小飞侠
     *    created: 2024.10.22 20:33:20
     */
    #include <bits/stdc++.h>
    using namespace std;
    #define ls u << 1
    #define rs u << 1 | 1
    #define LL long long
    #define int long long
    #define PII pair <int, int>
    #define fi first
    #define se second
    #define pub push_back
    #define pob pop_back
    #define puf push_front
    #define pof pop_front
    #define lb lower_bound
    #define ub upper_bound
    #define i128 __int128
    #define pcnt(x) __builtin_popcount(x)
    #define mem(a,goal) memset(a, (goal), sizeof(a))
    #define rep(x,start,end) for(int x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); ((start) < (end) ? x ++ : x --))
    #define aLL(x) (x).begin(), (x).end()
    #define sz(x) (int)(x).size()
    const int INF = 998244353;
    const int P = 1e9 + 7;
    const int N = 2e5 + 5;
    int a[N];
    int cnt[200][N];
    int ans;
    int n;
    
    void solve()
    {
        cin >> n;
        string s;
        cin >> s;
        for(int i = 0; i < n; ++ i) cnt[s[i] - 'a'][i] ++;
    
        for(int i = 0; i < 26; ++ i)
        {
            for(int j = 1; j < n; ++ j)
            {
                cnt[i][j] += cnt[i][j - 1];
            }
        }
    
        for(int i = 0; i < n; ++ i)
        {
            int t = s[i] - 'a';
            for(int j = 0; j < 26; ++ j)
            {
                if(j == t) continue;
                ans += (cnt[j][n - 1] - cnt[j][i]) * ((cnt[j][n - 1] - cnt[j][i]) - 1)/ 2;
            }
        }
    
        cout << ans << '\n';
    }
    signed main()
    {
        solve();
        return 0;
    }
    
    • 1

    信息

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