Codeforces 1238D. AB-string
求合法的串看一眼很不可做
考虑一下总方案减去不合法方案
考虑如何求不合法的串,首先串中连续的相同字符一定是回文串的一部分
然后考虑 $AB$ 交错的情况,发现对于某个 $A$ 它如果左右都有 $B$ 那么一定也是回文串的一部分
对于 $B$ 也是同理
那么只要考虑一段 $A$ 和一段 $B$ 连在一起的情况,发现当 $ABBBBB...$ 的时候,串是不合法的
当然 $BAAAAA...$ ,$AAAA...B$,$BBBB...A$ 也都是不合法的,其他情况显然都是合法的
然后所有情况都考虑完了,计算不合法情况很简单,直接看代码吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=3e5+;
int n;
ll Ans;
char s[N];
int main()
{
n=read(); scanf("%s",s+);
int pre=; Ans=1ll*n*(n-)/;
for(int i=;i<=n;i++)
{
if(s[i]==s[i-]) continue;
Ans++;//注意这里 Ans++ 是因为 'AB'或'BA' 之后会再次被减去
Ans-=(i-pre);
pre=i;
}
pre=n;
for(int i=n-;i>=;i--)
{
if(s[i]==s[i+]) continue;
Ans-=(pre-i); pre=i;
}
printf("%lld\n",Ans);
return ;
}
Codeforces 1238D. AB-string的更多相关文章
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力
Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consist ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- Codeforces - 1117E - Crisp String - 进制 - 交互
https://codeforces.com/problemset/problem/1117/E 就用abc表示数字来给每个数编码,编完直接问出移动的结果,反构造就行了,比C和D还简单. #inclu ...
- codeforces 828 C. String Reconstruction(思维+优先队列)
题目链接:http://codeforces.com/contest/828/problem/C 题解:有点意思的题目,可用优先队列解决一下具体看代码理解.或者用并查集或者用线段树都行. #inclu ...
- codeforces 779 D. String Game(二分)
题目链接:http://codeforces.com/contest/779/problem/D 题意:给你一段操作序列,按顺序依次删掉字符串1中相应位置的字符,问你最多能按顺序删掉多少个字符,使得s ...
- [SOJ #687]双生串(2019-11-6考试)/[hdu5431]AB String
题目大意 把所有仅包含\(AB\)的字符串按字典序排列,给你一个仅包含\(AB\)的字符串\(S\),然后有\(Q\)个问题,第\(i\)个问题给你\(k_i\),求不是\(S\)的子串中,第\(k_ ...
- Codeforces 494B Obsessive String
http://www.codeforces.com/problemset/problem/494/B 题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串. 思路:f[n] ...
随机推荐
- 使用io/ioutil进行读写文件
读文件: package main import ( "fmt" "io/ioutil" ) func main() { b, err := ioutil.Re ...
- linux(centos7)下安装maven
Linux下安装maven 1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用yum命令: 2.进入下载 ...
- div设置百分比高度 宽度
给div按百分比设置高度 宽度两种方法: 第一种是给body标签设置他的高度值,xxxpx,div就会根据body的像素值取百分比: 第二种方法就是在div属性中加入 position:absolut ...
- Nginx数据结构之散列表
1. 散列表(即哈希表概念) 散列表是根据元素的关键码值而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录, 以加快查找速度.这个映射函数 f 叫做散列方法,存放记录的数 ...
- 百度地图API--百度地图底色选择
可选择底色列表<select id="stylelist" onchange="changeMapStyle(this.value)"> <o ...
- ubuntu 14.04 升级到18.04
http://www.360doc.com/content/18/0929/09/35082563_790606785.shtml
- Manifest merger failed with multiple errors, see logs
Manifest merger failed with multiple errors, see logs 错误解决 合并 manifest 错误. https://blog.csdn.net/u01 ...
- hadoop新旧节点
注意:黑白名单只出现在名称(nn)节点<!-- 白名单 --><property><name>dfs.hosts</name>/Users/yangya ...
- PHP学习之验证码类
<?php $code = new Code(); $code->outImage(); class Code { //验证码个数 protected $number; //验证码类型 p ...
- HAproxy负载均衡-ACL篇
ACL定制法则: 开放策略:拒绝所有,只开放已知 拒绝策略:允许所有,只拒绝某些 事实上实现安全策略,无非也就是以上两种方法 redirect 参考:http://cbonte.github.io/h ...