题解 | #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")