链接:

https://codeforces.com/contest/1238/problem/D

题意:

The string t1t2…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:

t = AABBB (letters t1, t2 belong to palindrome t1…t2 and letters t3, t4, t5 belong to palindrome t3…t5);

t = ABAA (letters t1, t2, t3 belong to palindrome t1…t3 and letter t4 belongs to palindrome t3…t4);

t = AAAAA (all letters belong to palindrome t1…t5);

You are given a string s of length n, consisting of only letters A and B.

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

思路:

考虑ABB这种无法满足, 可以推出一只有一个a或b且处于子串的左右两边的时候,不满足, 记录不满足的个数,减一下.

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 3e5+10; char s[MAXN];
int n; int main()
{
cin >> n;
cin >> (s+1);
LL cnt = 0;
for (int i = 1, j;i < n;i = j)
{
j = i;
while (j+1 <= n && s[j+1] == s[i])
j++;
i = j;
while (j+1 <= n && s[j+1] != s[i])
j++;
cnt += j-i;
}
for (int i = n, j;i > 1;i = j)
{
j = i;
while (j-1 >= 1 && s[j-1] == s[i])
j--;
i = j;
while (j-1 >= 1 && s[j-1] != s[i])
j--;
if (i-j-1 > 0)
cnt += i-j-1;
}
LL ans = (1LL*n*(n-1))/2-cnt;
cout << ans << endl; return 0;
}

Educational Codeforces Round 74 (Rated for Div. 2) D. AB-string的更多相关文章

  1. Educational Codeforces Round 74 (Rated for Div. 2) C. Standard Free2play

    链接: https://codeforces.com/contest/1238/problem/C 题意: You are playing a game where your character sh ...

  2. Educational Codeforces Round 74 (Rated for Div. 2) B. Kill 'Em All

    链接: https://codeforces.com/contest/1238/problem/B 题意: Ivan plays an old action game called Heretic. ...

  3. Educational Codeforces Round 74 (Rated for Div. 2) A. Prime Subtraction

    链接: https://codeforces.com/contest/1238/problem/A 题意: You are given two integers x and y (it is guar ...

  4. Educational Codeforces Round 74 (Rated for Div. 2)

    传送门 A. Prime Subtraction 判断一下是否相差为\(1\)即可. B. Kill 'Em All 随便搞搞. C. Standard Free2play 题意: 现在有一个高度为\ ...

  5. Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】

    A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...

  6. Educational Codeforces Round 74 (Rated for Div. 2)补题

    慢慢来. 题目册 题目 A B C D E F G 状态 √ √ √ √ × ∅ ∅ //√,×,∅ 想法 A. Prime Subtraction res tp A 题意:给定\(x,y(x> ...

  7. Educational Codeforces Round 74 (Rated for Div. 2)E(状压DP,降低一个m复杂度做法含有集合思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100005];int pos[ ...

  8. Educational Codeforces Round 94 (Rated for Div. 2) C. Binary String Reconstruction (构造)

    题意:给你一个字符串\(s\),原字符串为\(w\),如果\(i>x\)且\(w_{i-x}=1\),那么\(s_{i}=1\),如果\(i+x\le n\)且\(w_{i+x}=1\),那么\ ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. redis事务、并发及应用场景

    目录 事务概念 事务命令 乐观锁 悲观锁 并发控制及过期时间 队列 队列防丢失 阻塞队列 时间区间控制 持久化 RDB AOF 命令追加 文件写入.同步 RDB.AOF优缺点 RDB优缺 AOF优缺 ...

  2. (四)Resquest 知识点总结 (来自那些年的笔记)

    目录 URL和URI的区别 获取URL.URI 什么是HttpServletResquest 获取请求头中字段的内容 获取请求信息的数据 将客户机的请求变为一个流返回 常用的方法 request乱码问 ...

  3. php之简单算法

    选择排序 方式:先让第一位与其他位比较大小找到最小的数字,然后是第二位与除第一位的其他位比较大小找出第二位,依此类推 $arr = [2,45,12,67,33,5,23,132,46]; for ( ...

  4. ORACLE 的前后台进程

    关于oracle用户进程,服务进程,后台进程 用户进程(User Process) 是一个需要与Oracle Server交互的程序 运行于客户端 当用户运行某个工具或应用程序(如SQL*Plus)时 ...

  5. Django ORM 高性能查询优化

    一.QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Entry.objects.all( ...

  6. PAT(B) 1006 换个格式输出整数(Java)

    题目链接:1006 换个格式输出整数 (15 point(s)) 代码 /** * Score 15 * Run Time 153ms * @author wowpH * @version 1.1 * ...

  7. 1261: 单位转换(Java)

    WUSTOJ 1261: 单位转换 参考资料 数字字符串拆分--百度知道 Description BobLee最近在复习考研,在复习计算机组成原理的时候,遇到了一个问题.就是在计算机存储里面的单位转换 ...

  8. Redis的bind的误区(转)

    原文1:https://blog.csdn.net/cw_hello1/article/details/83444013 原文2:https://www.cnblogs.com/suiyueqiann ...

  9. (七)装配Bean(1)

    针对给接口提供哪一个具体的实现,也就是装配哪一种具体的实现bean,在Spring中提供了多种方式,主要包括3种: 一.隐式的bean发现机制和自动装配(自动化装配bean) 二.在Java类中进行显 ...

  10. 记录我第一篇用Markdown写的Blog

    Markdown的介绍 喝水不忘挖井人-Markdown的创造者 Markdown 最初是由 John Gruber 和 Aaron Swartz 于 2004 年共同设计的(在这里插一句,Aaron ...