ZOJ - 3985 - String of CCPC (思维 + 暴力)
2024-08-30 23:23:20
题意:
询问一共有有多少个CCPC,每个得1分,可以自己在任意位置添加字母,第i次添加需要耗费i-1分
思路:
既然每次添加需要耗分,添加第二个字母,相当于没有添加,所以只需要添加一次就好
先计算出原始字符串中的CCPC,在不破坏CCPC的前提下,添加字母即可
代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 2e5+10;
int flag[maxn];
char str[maxn];
int main() {
int t, n, ans, temp;
scanf("%d", &t);
while(t--) {
ans = temp = 0;
scanf("%d %s", &n, str+1);
for(int i = 1; i <= n; i++)
flag[i] = 0;
for(int i = 1; i <= n-3; i++) {
if(str[i]=='C' && str[i+1]=='C' && str[i+2]=='P' && str[i+3]=='C')
ans++, flag[i] = 1;
}
for(int i = 1; i <= n-2; i++) {
if(flag[i] || temp) continue;
if(str[i]=='C' && str[i+1]=='C' && str[i+2]=='C' && flag[i+1]==0) temp = 1;
else if(str[i]=='C' && str[i+1]=='P' && str[i+2]=='C' && flag[i-1]==0) temp = 1;
else if(str[i]=='C' && str[i+1]=='C' && str[i+2]=='P') temp = 1;
}
printf("%d\n", ans+temp);
}
return 0;
}
最新文章
- js获取css中的样式
- 浅谈webWorker
- ACM 杭电HDU 2084 数塔 [解题报告]
- UIViewContentMode各类型效果
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
- python从Microsoft Excel文件中导入数据
- ORACLE函数大全
- if分支的四种形式
- VHDL学习记录
- 分布式高性能消息处理中心HPMessageCenter
- jQuery---过滤选择器
- python摸爬滚打之day16----类的成员
- Elasticsearch基础知识要点QA
- windows 执行bat脚本
- WEB开发库收集
- MyEclipse同时配置多个tomcat
- (转)找回Git中丢失的Commit
- JVM gc介绍
- LNMP下安装memcache
- 2017-2018-1 20155207&;20155308《信息安全技术》实验四-木马及远程控制技术