小红的子串权值和【Python和C++实现】

360影视 动漫周边 2025-08-05 19:41 3

摘要:n = int(input.strip)s = input.striptotal_substrings = n * (n + 1) // 2ans = total_substringsfor i in range(n - 1): if s[i] != s[i

n = int(input.strip)s = input.striptotal_substrings = n * (n + 1) // 2ans = total_substringsfor i in range(n - 1): if s[i] != s[i + 1]: left_choices = i + 1 right_choices = n - i - 1 ans += left_choices * right_choicesprint(ans)#include#includeusing namespace std;int main { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n; string s; cin >> n; cin >> s; long long total_substrings = n * (n + 1) / 2; long long ans = total_substrings; for (long long i = 0; i

来源:易道

相关推荐