日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AB-string CodeForces - 1238D(思维)

發布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AB-string CodeForces - 1238D(思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

The string t1t2…tkt1t2…tk is good if each letter of this string belongs to at least one palindrome of length greater than 1.

A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.

Here are some examples of good strings:

tt = AABBB (letters t1t1, t2t2 belong to palindrome t1…t2t1…t2 and letters t3t3, t4t4, t5t5 belong to palindrome t3…t5t3…t5);
tt = ABAA (letters t1t1, t2t2, t3t3 belong to palindrome t1…t3t1…t3 and letter t4t4 belongs to palindrome t3…t4t3…t4);
tt = AAAAA (all letters belong to palindrome t1…t5t1…t5);
You are given a string ss of length nn, consisting of only letters A and B.

You have to calculate the number of good substrings of string ss.

Input
The first line contains one integer nn (1≤n≤3?1051≤n≤3?105) — the length of the string ss.

The second line contains the string ss, consisting of letters A and B.

Output
Print one integer — the number of good substrings of string ss.

Examples
Input
5
AABBB
Output
6
Input
3
AAA
Output
3
Input
7
AAABABB
Output
15
Note
In the first test case there are six good substrings: s1…s2s1…s2, s1…s4s1…s4, s1…s5s1…s5, s3…s4s3…s4, s3…s5s3…s5 and s4…s5s4…s5.

In the second test case there are three good substrings: s1…s2s1…s2, s1…s3s1…s3 and s2…s3s2…s3.
題意:good string 是源字符串的一個子串。對于一個good string定義是醬紫的,這個字符串中的每一個字母,都位于一個長度大于等于2的回文串中,問有多少個good string。
思路:對于一條字符串來說,一共有n*(n-1)/2個字符串。我們要求有多少個good string,那么我們可以去求有多少個字符串不是good string。相減就可以了。
對于ab,ba這樣的來說,就是good string,但是對于abbbbb,bbbbba這樣的來說,就不是good string 。
所以我們可以正著求一遍,求出bbbbbba這樣的,反著求一遍,求出abbbbb這樣的,但是ab或者ba會被減兩遍,再遍歷一遍加上就好了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=3e5+100; char s[maxx]; int n;int main() {scanf("%d%s",&n,s);ll ans=1ll*n*(n-1)/2ll;int cnt=1;for(int i=1;i<n;i++){if(s[i]==s[i-1]) cnt++;else{ans-=(ll)cnt;cnt=1;}}cnt=1;for(int i=n-2;i>=0;i--){if(s[i]==s[i+1]) cnt++;else{ans-=(ll)cnt;cnt=1;}}for(int i=1;i<n;i++) if(s[i]!=s[i-1]) ans++;cout<<ans<<endl; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的AB-string CodeForces - 1238D(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。