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(思维)相关推荐

  1. String类常用方法思维导图总结

    String类常用方法思维导图总结 给自己加油,保持每天都学习,更新博客!信Java得永生!

  2. Minimum Ternary String CodeForces - 1009B(思维)

    You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...

  3. String[]转化暴露“思维误区”

    那天写code,用到这个,强转,将页面传来的值转换为数组,结果是,当页面传来的只有一个值时,它是无法转换为数组的,只能获得1个string,只有length>1时才会转化为数组的形式,报的错误是 ...

  4. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstd ...

  5. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  6. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  7. Generate a String CodeForces - 710E(dp)

    zscoder wants to generate an input file for some programming competition problem. His input is a str ...

  8. Vasya and String CodeForces - 676C(尺取法)

    High school student Vasya got a string of length n as a birthday present. This string consists of le ...

  9. K-th Beautiful String CodeForces - 1328B(二分+数学)

    For the given integer n (n>2) let's write down all the strings of length n which contain n−2 lett ...

最新文章

  1. SPOJ - BITDIFF: Bit Difference [神妙の预处理]
  2. 一大波物联网僵尸正在袭来,都有啥安全保护方法?
  3. pjsip在没有mic的情况下听到对方的声音
  4. *8.哈希冲突是什么?以及如何解决哈希冲突
  5. 阿里Python后端1w+薪资面试真题!(附带准答案)offer轻松拿到手
  6. php产生订单号不重复,php如何生成不重复订单号
  7. 三维卷积伪代码_用于视频超分辨率的可变形三维卷积
  8. Endnote导入中文文献格式
  9. 《缠中说禅108课》25:吻,MACD、背弛、中枢
  10. 神来之笔之傅里叶变换(频谱)
  11. Linux7安装oracle11g报错 Error in invoking target ‘agent nmhs‘ of makefile “ins_emagent.mk”
  12. iOS 聊天表情键盘
  13. 【JAVA虚拟机】java虚拟机
  14. python做马尔科夫模型预测法_python 日常笔记 hmmlearn 隐性马尔科夫模型案例分析...
  15. 智能制造并非只是自动化
  16. BI数据分析专业词汇整理(1/9)
  17. 如何将matlab中文版本转换为英文界面
  18. 【CSDN云IDE】个人使用体验和建议(含超详细操作教程)(python、webGL方向)
  19. 大数据数据科学家常用面试题_想要成为数据科学家,解决数据科学面试的简单指南...
  20. vue 计数器_Vuex 使用及简单实例(计数器)

热门文章

  1. dart初始化项目的分析
  2. 2021.4.23最新mac11.1 big sur 关于CocoaPods安装和使用
  3. IOS模拟登录之沙盒原理
  4. mysql代表单个字符_mysql字符编码以及字符序
  5. java pdfbox2 中文乱码,如何从PDFBox 2.0.2中的PDDocument提取字体
  6. python的xpath用法_Python爬虫杂记 - Xpath高级用法
  7. 根据数据库表gengxin实体类_ASP.NET开发实战——(十二)数据库之EF Migrations
  8. C++11标准出现后,函数指针写法汇总
  9. 最详细的linux下的磁盘分区及格式化
  10. Android studio3.0开启抓包功能打包会使apk体积增大好几倍