题解 | #abb#

abb

https://www.nowcoder.com/practice/0a8bbf8b9b5b4280957849ef4f240f07

#include <bits/stdc++.h>
using namespace std;
long long a[100100][26];
int main() {
    int n;
    cin>>n;
    string str;
    cin>>str;
    for(int i=n-1;i>=0;i--)
    {
       for(int j=0;j<26;j++)
       {
        a[i][j]=a[i+1][j];
       } 
       a[i][str[i]-'a']++;
    }
    long long s=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<26;j++)
        {
            if(j!=str[i]-'a')
            {
                 s+=a[i+1][j]*(a[i+1][j]-1)/2;
            }
        }
    }
    cout<<s;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务