RGB Triplets(后缀和)
题目描述
We have a string S of length N consisting of R, G, and B.
Find the number of triples (i, j, k) (1≤i<j<k≤N) that satisfy both of the following conditions:
·Si≠Sj, Si≠Sk, and Sj≠Sk.
·j−i≠k−j.
Constraints
·1≤N≤4000
·S is a string of length N consisting of R, G, and B.
输入
Input is given from Standard Input in the following format:
N
S
输出
Print the number of triplets in question.
样例输入
【样例1】
4
RRGB
【样例2】
39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB
样例输出
【样例1】
1
【样例2】
1800
提示
样例1解释:
Only the triplet (1, 3, 4) satisfies both conditions. The triplet (2, 3, 4) satisfies the first condition but not the second, so it does not count.
思路:
暴力做法,直接三层循环 时间复杂度:O(n^3)
数据范围: n <= 4000 会超时
只开两个for循环的做法:
通过后缀和来代替第三层循环判断是否满足条件一
最后再把多算的减去即可(满足条件一但是不满足条件二)
AC代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>using namespace std;typedef long long ll;ll n,ans;ll exist_r[4010],exist_g[4010],exist_b[4010];string str;int main()
{cin >> n >> str;for(ll i = n-1; i >= 0; i--){exist_r[i+1] = exist_r[i+2];exist_g[i+1] = exist_g[i+2];exist_b[i+1] = exist_b[i+2];if(str[i] == 'R') exist_r[i+1]++;else if(str[i] == 'G') exist_g[i+1]++;else exist_b[i+1]++;}for(ll i = 1; i <= n; i++){for(ll j = i+1 ; j <= n; j++){if(str[i-1] == 'B' && str[j-1] == 'G') ans += exist_r[j+1];if(str[i-1] == 'G' && str[j-1] == 'B') ans += exist_r[j+1];if(str[i-1] == 'B' && str[j-1] == 'R') ans += exist_g[j+1];if(str[i-1] == 'R' && str[j-1] == 'B') ans += exist_g[j+1];if(str[i-1] == 'R' && str[j-1] == 'G') ans += exist_b[j+1];if(str[i-1] == 'G' && str[j-1] == 'R') ans += exist_b[j+1];}}for(ll i = 0; i < n; i++){for(ll d = 1; d <= n; d++){if(i+d > n-1 || i+d+d > n-1) continue; //防止数组越界if(str[i] != str[i+d] && str[i] != str[i+d+d] && str[i+d] != str[i+d+d]) ans--;}}cout << ans << endl;return 0;
}
RGB Triplets(后缀和)相关推荐
- JZ2440 数码相框项目 扩展项目(一) 多文件图标 (二) 显示png
文章目录 链接 扩展项目一 1.目标 2.分析 3.实现 4.效果 扩展项目二 1.目标 2.分析 3.实现 a.分配注册子类结构体 b.判断文件是否为png c.获取png文件格式 4.效果 链接 ...
- 图片和ppm文件互转
一.代码结构 二.代码实现 Denoise.java: package com.xj.ppm.toimg;import java.awt.FlowLayout; import java.awt.ima ...
- 嵌入式Linux小项目之图片编解码播放器(6)
目录 一.解码显示png图片 1.思路分析 2.libpng移植 3.zlib移植 4.参考源码包自带的资料 5.学习了解示例代码 6.完整移植好的代码 一.解码显示png图片 1.思路分析 (1)p ...
- 我在atcoder打比赛
我在atcoder打比赛 AtCoder Beginner Contest 177 比赛人数9636 AtCoder Beginner Contest 177 A Don't be late 化浮 ...
- UPC-2021个人训练赛第20场-部分题解
目录 RGB Triplets 题目描述 输入 输出 样例输入 Copy 样例输出 Copy 提示 Select Half 题目描述 输入 输出 样例输入 Copy 样例输出 Copy 提示 心灵的抚 ...
- Java面试题全集(C)
这部分主要是开源Java EE框架方面的内容,包括Hibernate.MyBatis.Spring.Spring MVC等,由于Struts 2已经是明日黄花,在这里就不讨论Struts 2的面试题, ...
- pymol 知道多少?pymol技巧汇总
基本命令(也可使用鼠标操作,但不如命令来得简单) pwd # show current directory dir # list file in the curre ...
- linux 读取png图片大小,使用libpng读取PNG图片像素数据
附录 让我们打开pnglib下面的一个example.c /* Read a PNG file. You may want to return an error code if the read * ...
- Matlab 散点图(Scatter)和颜色图(Colormap)
目录 一.什么是散点图? 二.什么数据需要用散点图来呈现? 三.matlab 中的颜色如何表示和调用? 四.颜色和透明度在散点图中的灵活应用 二维散点图 三维散点图 数据分组! 五.Scatter 的 ...
- Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
场景 使用Node搭建一个静态资源服务器,使其根据请求不同的文件类型设置不同的响应头. 比如: ".png":"image/png" , ".png& ...
最新文章
- 《从零开始学Swift》学习笔记(Day 52)——Cocoa错误处理模式
- 笔试训练第二次知识点汇总
- 线性代数之相似矩阵与二次型基础点
- 《Effective C#》读书笔记——条目10:使用可选参数减少方法重载的数量C#语言习惯...
- python编程画布_Python Tkinter 画布(Canvas)
- JDBC08时间处理
- 听音乐是运用计算机的技术,计算机音乐技术在电影音乐中的运用
- summer 's wonderful so why not SMILE
- finder个人收藏和前往文件夹
- Kubernetes教程之跟着官方文档从零搭建K8S
- 淘宝短视频多模态融合识别
- 蓝桥杯:历年试题PREV-55—小计算器
- 用mac的chrome浏览器调试 Android 手机的网页
- python是跨平台的 以及 py、pyc、pyo
- Dart vs Swift
- 增加在线日语词典3个 - 最新版 - 超级网际搜索(SuperSearch) - 让思考从搜索开始!
- 信阳市浉河区最美奋斗者
- mac 连接阿里云服务 ssh 一会自动断开
- 时尚秀9元港韩混搭服饰K847弃用职业模特 让真实的女人上封面
- java easypoi使用模板导出Excel,合并单元格
热门文章
- 传雅虎考虑收购新闻摘要应用Summly
- 听课笔记-《计算机科学速成课》5-9计算机硬件
- bom实现方块移动_HTML 方块移动
- 移动方块java,技术编辑教你解决Java移动方块触碰边界反弹
- 「Python入门」Python多进程
- Minimum supported Gradle version is 4.6. Current version is 4.4.
- java 为pdf添加水印图片
- 【NLP】NO5:文本聚类
- 国防科技大学计算机专业戴眼镜,国防科技大学的男生毕业照流出,被女网友们狂赞:“把我扔进去”...
- xshell 导入.xsh 文件