Miraitowa


私信TA

用户名:20yxjiang1

访问量:743

签 名:

等  级
排  名 14650
经  验 819
参赛次数 0
文章发表 1
年  龄 21
在职情况 学生
学  校 STU
专  业 CS

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>

#include<string>

using namespace std;


int leftbound[100000];

int rightbound[100000];


int main(){

    int sum = 0;

    string str;

    cin>>str;

    int len = str.size();

    int arrright[26] = {0};

    int countright[26] = {0};


    for(int j = len-1 ; j >= 0 ; j--){

        int r = str[j]-'a';

        countright[r]++;

        if(countright[r] == 1){

            rightbound[j] = len-j;

        }else{

            rightbound[j] = arrright[r] - j;

        }

        arrright[r] = j;

    }


    int arrleft[26] = {0};

    int countleft[26] = {0};

    for(int i = 0 ; i < len ; i++){

        int l = str[i]-'a';

        countleft[l]++;

        if(countleft[l] == 1){

            leftbound[i] = i + 1;

        }else{

            leftbound[i] = i - arrleft[l];

        }

        arrleft[l] = i;

        sum = sum + leftbound[i] + rightbound[i] - 1 + (leftbound[i]-1)*(rightbound[i]-1);

    }

   

    cout<<sum<<endl;

    return 0;

}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区