蓝桥杯 子串分值和

问题描述

输入格式

输入一行包含一个由小写字母组成的字符串S。

输出格式

输出一个整数表示答案。

样例输入

ababc

样例输出

28

评测用例规模与约定

#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>
using namespace std;
const int MAXN = 100 + 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 l[26]; //某字母在字符串中上一次出现的位置
int main(){
    string s;
    cin >> s;
    int len = s.size();
    memset(l, -1, sizeof l);
    LL ans = 0;
    for(int i = 0; i < len; ++i){
        ans += (LL)(i - l[s[i] - 'a']) * (len - i);
        l[s[i] - 'a'] = i;
    }
    printf("%lld\n", ans);
    return 0;
}
posted @ 2021-04-17 21:31  Somnuspoppy  阅读(569)  评论(0编辑  收藏  举报