Educational Codeforces Round 74 (Rated for Div. 2) D. AB-string
链接:
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的更多相关文章
- 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 ...
- 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. ...
- 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 ...
- Educational Codeforces Round 74 (Rated for Div. 2)
传送门 A. Prime Subtraction 判断一下是否相差为\(1\)即可. B. Kill 'Em All 随便搞搞. C. Standard Free2play 题意: 现在有一个高度为\ ...
- Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】
A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...
- Educational Codeforces Round 74 (Rated for Div. 2)补题
慢慢来. 题目册 题目 A B C D E F G 状态 √ √ √ √ × ∅ ∅ //√,×,∅ 想法 A. Prime Subtraction res tp A 题意:给定\(x,y(x> ...
- 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[ ...
- 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\),那么\ ...
- 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 ...
随机推荐
- 【转帖】iPhone 11 Pro Max皇帝版物料成本不足3500元 卖一赚二
iPhone 11 Pro Max皇帝版物料成本不足3500元 卖一赚二 https://www.cnbeta.com/articles/tech/894449.htm 供应链的掌控力很重要 苹果今年 ...
- VMware的下载与安装
VMware的下载与安装 一.虚拟机的下载 1.进入VMware官网,点击左侧导航栏中的下载,再点击图中标记的Workstation Pro,如下图所示. 2.根据操作系统选择合适的产品,在这里以Wi ...
- 利用Python进行数据分析_Pandas_基本功能
申明:本系列文章是自己在学习<利用Python进行数据分析>这本书的过程中,为了方便后期自己巩固知识而整理. 第一 重新索引 Series的reindex方法 In [15]: obj = ...
- WUSTOJ 1208: 计算整数四则运算表达式的结果(Java)
1208: 计算整数四则运算表达式的结果 参考资料 数据结构(C语言版)严蔚敏 吴伟民 编著----表达式求值 题目 简单四则运算.更多内容点击标题. 保证表达式合法. 运算符只包含:加(+),减 ...
- redis源码解读--内存分配zmalloc
目录 主要函数 void *zmalloc(size_t size) void *zcalloc(size_t size) void zrealloc(void ptr, size_t size) v ...
- post请求 大坑 注意点
如果后台接收的是一个list 前台请求的时候 PLdelete() { //批量删除流量计 console.log(this.multipleSelection instanceof Array,9) ...
- CentOS 系统下使用 yum 安装 Redis
本文主要介绍在 CentOS 7 系统下使用 yum 安装 Redis 的过程. 更改 yum 源 将 Centos 的 yum 源更换为国内的阿里云源. 首先备份你的原镜像文件,保证出错后可以恢复: ...
- docker 学习1 WSL docker ,Windows docker
获取Linux内核版本 //使用 lsb_release -a 可见我电脑上的 WSL Linux 版本是 Ubuntu的. 安装docker for ubuntu (遇到问题) 转[http://b ...
- 学习笔记-Rabin-Karp哈希
在数学一本通上看过这两人名字,现在又出现了... 思想: 用一个整数表示一个字符串 \(w_{str}\)=(\(a_0\) \(p^{n-1}\)+\(a_1\) \(p^{n-2}\)+...+\ ...
- Go part 5 结构体,方法与接收器
结构体 结构体定义 结构体的定义只是一种内存布局的描述(相当于是一个模板),只有当结构体实例化时,才会真正分配内存空间 结构体是一种复合的基本类型,通过关键字 type 定义为 自定义 类型后,使结构 ...