• 蓝桥杯 子串分值


    问题描述

    输入格式

    输出格式

    输出一个整数表示答案。

    样例输入

    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;
    }
    
  • 相关阅读:
    POJ 1141 括号匹配 DP
    881. Boats to Save People
    870. Advantage Shuffle
    874. Walking Robot Simulation
    文件操作
    861. Score After Flipping Matrix
    860. Lemonade Change
    842. Split Array into Fibonacci Sequence
    765. Couples Holding Hands
    763. Partition Labels
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/14672285.html
Copyright © 2020-2023  润新知