对于一个长度为len的字符串,它的所有子串数量为
(
l
e
n
∗
(
l
e
n
+
1
)
/
2
)
(len*(len+1)/2)
(len∗(len+1)/2)
AC代码
voidsolve(){
map<char,vector<int>> m;
string s;cin>>s;
s='0'+s;for(int i =1; i < s.size();++i){if(m[s[i]].empty()) m[s[i]].push_back(0);
m[s[i]].push_back(i);}int ans =0;int otot = s.size()*(s.size()-1)/2;for(char i ='a'; i <='z';++i){if(m[i].empty())continue;int tot =0;
vector<int> temp = m[i];
temp.push_back(s.size());for(int j =0; j < temp.size()-1;++j){int len = temp[j+1]-temp[j]-1;
tot +=(len*(len+1)/2);}
ans+=(otot-tot);}
cout<<ans<<endl;}