问题描述
输入格式
输出格式
输出一个整数表示答案。
样例输入
ababc
样例输出
21
评测用例规模与约定
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<string>
#include<sstream>
#define pr(x) cout << #x << ": " << x << " "
#define prln(x) cout << #x << ": " << x << endl
using namespace std;
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
int dr[] = {0, 0, -1, 1};
int dc[] = {1, -1, 0, 0};
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
int lc[26]; //某字母在字符串中左边最近一次出现的位置
int rc[26];
int r[MAXN]; //某字母在字符串中右边最近一次出现的位置
char s[MAXN];
int main(){
scanf("%s", s);
int len = strlen(s);
for(int i = 0; i < 26; ++i){
lc[i] = -1;
rc[i] = len;
}
for(int i = 0; i < len; ++i){
r[i] = len;
}
for(int i = len - 1; i >= 0; --i){
r[i] = rc[s[i] - 'a'];
rc[s[i] - 'a'] = i;
}
LL ans = 0;
for(int i = 0; i < len; ++i){
ans += (LL)(i - lc[s[i] - 'a']) * (r[i] - i);
lc[s[i] - 'a'] = i;
}
printf("%lld
", ans);
return 0;
}