浏览器标题切换
浏览器标题切换end

CodeForces-1238D-AB-string CodeForces-思维+字符串反向思考

The string t1t2tkt1t2…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 t1t2t1…t2 and letters t3t3, t4t4, t5t5 belong to palindrome t3t5t3…t5);
  • tt = ABAA (letters t1t1, t2t2, t3t3 belong to palindrome t1t3t1…t3 and letter t4t4 belongs to palindrome t3t4t3…t4);
  • tt = AAAAA (all letters belong to palindrome t1t5t1…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 (1n31051≤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: s1s2s1…s2, s1s4s1…s4, s1s5s1…s5, s3s4s3…s4, s3s5s3…s5 and s4s5s4…s5.

In the second test case there are three good substrings: s1s2s1…s2, s1s3s1…s3 and s2s3s2…s3.

 

复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 const int N=300020;
 4 typedef long long ll;
 5 
 6 char a[N],b[N];
 7 
 8 int main()
 9 {
10     ll n;
11     while(~scanf("%lld",&n))
12     {
13         scanf("%s",a);
14         ll sum1=0,sum2=0,ss=0;
15         int k1=0,k2=0;
16         for(int i=1;i<n;i++)
17         {
18             if(a[i]!=a[i-1])
19             {
20                 ss++;
21                 sum1=sum1+i-k1;
22                 k1=i;
23             }
24         }
25 
26         int p=0;
27         for(int i=n-1;i>=0;i--)
28             b[p++]=a[i];
29         for(int i=0;i<p;i++)
30         {
31             if(b[i]!=b[i-1])
32             {
33                 sum2=sum2+i-k2;
34                 k2=i;
35             }
36         }
37         ll w=n*(n-1)/2-sum1-sum2+ss;
38         printf("%lld\n",w);
39     }
40     return 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
41 }
复制代码

 

posted @   抓水母的派大星  阅读(255)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示