【样例说明】
子串 f值
a 1
ab 2
aba 2
abab 2
ababc 3
b 1
ba 2
bab 2
babc 3
a 1
ab 2
abc 3
b 1
bc 2
c 1
参考博客:https://blog.csdn.net/weixin_45483201/article/details/109137296?utm_source=app
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int last[30], n;
char str[N];
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
scanf("%s", str + 1);
n = strlen(str + 1);
ll ans = 0;
for(int i = 1; i <= n; i++) {
ans += 1ll * (i - last[str[i] - 'a']) * (n - i + 1);
last[str[i] - 'a'] = i;
}
printf("%lld\n", ans);
return 0;
}