唐山乾正建设工程材料检测公司网站,wordpress内部架构,那些网站用不着做优化,青岛谁优化网站做的好处思路#xff1a; 本题有点难想#xff0c;采用贡献和的思想。首先需要定义一个last数组#xff0c;用于记录当前遍历的字母s[i]上一次出现在字符串s中的位置。接着遍历数组#xff0c;计算每一个字母的贡献值。
参考代码#xff1a;
#includebits/stdc.h
#defi…思路 本题有点难想采用贡献和的思想。首先需要定义一个last数组用于记录当前遍历的字母s[i]上一次出现在字符串s中的位置。接着遍历数组计算每一个字母的贡献值。
参考代码
#includebits/stdc.h
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;int main()
{ios::sync_with_stdio(false); string s;cin s;ll tmp[26], last[100005]; //last数组记录该字母上次出现的位置 fill(tmp, tmp26, -1); int len s.size();for(int i 0; i len; i){last[i] tmp[s[i]-a];tmp[s[i]-a] i; }ll cnt 0;for(int i 0; i len; i){cnt (i-last[i]) * (len-i);}cout cnt;return 0;
}