原题链接
2872. 子串分值和 - AcWing题库
代码
//dp dp[i] 以第i个字母结尾的字符串分值和 //dp[i] = dp[i - 1] + i - ptr[t]; #include#include #include using namespace std; const int N = 100010; char s[N]; int dp[N]; int ptr[30]; int main() { cin >> s + 1; long long ans = 0; int len = strlen(s+1); for (int i = 1; i <= len;i++) { int t = s[i] - 'a'; dp[i] = dp[i - 1] + i - ptr[t]; ptr[t] = i; ans += dp[i]; } printf("%lld", ans); return 0; }



