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 ...
随机推荐
- linux网卡出现问题:Job for network.service failed because the control process exited with error code问题
[转自]:https://blog.csdn.net/dongfei2033/article/details/81124465 今天在centOS 7下更改完静态ip后发现network服务重启不了, ...
- ubuntu18和windows10双系统时间不同步问题(Ubuntu)
1.安装并校准时间 sudo apt install ntpdate sudo ntpdate time.windows.com 2.写入硬件配置 sudo hwclock --localtime - ...
- 剑指offer51:构建乘积数组B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1],不能使用除法
1 题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1] ...
- java实现带过期时间的缓存
private static ScheduledExecutorService swapExpiredPool = new ScheduledThreadPoolExecutor(10); priva ...
- Mongo DB分片
分片,指的就是把数据拆分,将其分散到不同机器上的过程.MongoDB支持自动分片,对应用而言,好像始终和一个单机的服务器交互一样. 分片和复制复制是让多台服务器拥有相同的数据副本,而分片是每个分片都拥 ...
- oracle 数据库 主键索引重建
oracle 数据库 主键索引重建 alter table table_name drop primary key; alter table table_name add constraint pk_ ...
- Session和Cookie的原理
1.session和cookie的存储 session一般保存在服务端文件中,php.ini中有个配置项--session.save_path='';这个里面填写的路径,将会使session文件保存在 ...
- 一个炒鸡好用的pdf阅读器
下载地址:https://www.sumatrapdfreader.org/free-pdf-reader.html 一个关系很好的同事推荐的pdf阅读器 之前用的感觉不错 每次都记不住 自己收 ...
- 日志(log4j2)
日志测试java代码如下: package com.learn.test; import org.apache.logging.log4j.LogManager; import org.apache. ...
- 初试Maven
1 [检查]确认已经安装jdk,已经环境变量中配置JAVA_HOME,已经修改Path 2 [下载]从http://maven.apache.org/download.cgi下载所需要的版本,笔者使用 ...