String of CCPC2017(CCPC秦皇岛站)

题目:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370125

BaoBao has just found a string (s) of length (n) consisting of ‘C’ and ‘P’ in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring (s_is_{i+1}s_{i+2}s_{i+3}) of (s) is “good”, if and only if (s_i = s_{i+1} = s_{i+3} =) ‘C’, and (s_{i+2} =) ‘P’, where (s_i) denotes the (i)-th character in string (s). The value of (s) is the number of different “good” substrings in (s). Two “good” substrings (s_is_{i+1}s_{i+2}s_{i+3}) and (s_js_{j+1}s_{j+2}s_{j+3}) are different, if and only if (i \ne j).

To make this string more valuable, BaoBao decides to buy some characters from a character store. Each time he can buy one ‘C’ or one ‘P’ from the store, and insert the character into any position in (s). But everything comes with a cost. If it’s the (i)-th time for BaoBao to buy a character, he will have to spend (i-1) units of value.

The final value BaoBao obtains is the final value of (s) minus the total cost of all the characters bought from the store. Please help BaoBao maximize the final value.

Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer (n) ((1 \le n \le 2\times 10^5)), indicating the length of string (s).

The second line contains the string (s) ((|s| = n)) consisting of ‘C’ and ‘P’.

It’s guaranteed that the sum of (n) over all test cases will not exceed (10^6).

Output
For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.

Sample Input
3
3
CCC
5
CCCCP
4
CPCP
Sample Output
1
1
1
Hint
For the first sample test case, BaoBao can buy one ‘P’ (cost 0 value) and change to “CCPC”. So the final value is 1 - 0 = 1.

For the second sample test case, BaoBao can buy one ‘C’ and one ‘P’ (cost 0 + 1 = 1 value) and change (s) to “CCPCCPC”. So the final value is 2 - 1 = 1.

For the third sample test case, BaoBao can buy one ‘C’ (cost 0 value) and change (s) to “CCPCP”. So the final value is 1 - 0 = 1.

It’s easy to prove that no strategies of buying and inserting characters can achieve a better result for the sample test cases.

一道不能说难的题,,开始以为就是插入几个字符会增加CCPC的个数,马上敲了一个过去,直接wa,,认真读题,要了快一个小时读懂了,,,一敲,感觉没啥问题,,已提交又错。。百度看了某位大佬的博客,发现他前后加了几个字符就防止越界了。。不过我怎么没发觉哪里有错。。

这段是照着改的
AC代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;


int vis[200050]; 

int main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int t; cin >> t;
	
	while(t--){
		
		int n; cin >> n;
		string str; cin >> str;
		
		memset(vis,0,sizeof(vis));
		
		int ans = 0;
		str = "   "+str+"   ";
		for(int i = 0;i+3<str.size();i++){	//求出原来有多少个 
			
			if(str[i] == 'C' && str[i+1] == 'C' && str[i+2] == 'P' && str[i+3] == 'C'){
				vis[i+1] = vis[i+2] = vis[i+3] = 1;
				ans++;
			}
		}
		
	

		for(int i = 3;i<=str.size()-3;i++){
			
			if(vis[i] == 0){
			
				if(str[i] == 'C' && str[i+1] == 'P' && str[i+2] == 'C'){
					ans++;
					break;
				}
			
			
				if(str[i-1] == 'C' && str[i] == 'P' && str[i+1] == 'C'){
					ans++;
					break;
				}
			
			
				if(str[i-2]=='C' && str[i-1] == 'C' && str[i] == 'C'){
					ans++;
					break;
				}
			
			
				if(str[i-3] == 'C' && str[i-2] == 'C' && str[i-1] == 'P'){
					ans++;
					break;
				}
			}
			
		}
		
		cout << ans << endl;
	}	
	
	return 0;
}

下面这个是原来的,不知道哪里错了,希望有大佬能帮我看看

#include <iostream>
#include <string>
#include <cstring>
using namespace std;


int main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int t; cin >> t;
	
	while(t--){
		
		int n; cin >> n;
		string str; cin >> str;
		
		int ans = 0;
		
		for(int i = 0;i+3<str.size();i++){	//求出原来有多少个 
			
			if(str[i] == 'C' && str[i+1] == 'C' && str[i+2] == 'P' && str[i+3] == 'C'){
				ans++;
			}
		}
		str =' ' + str + ' ';	//考虑第二和第四种情况
		int len = str.size();
		for(int i = 0;i<=len;i++){
			
			if(i+1<len && i+2<len){	//插入到第一个 
				if(str[i] == 'C' && str[i+1] == 'P' && str[i+2] == 'C'){
					ans++;
					break;
				}
			}
			if(i-1>=0 && i+1<len){	//插入到第二个 
				if(str[i-1] == 'C' && str[i] == 'P' && str[i+1] == 'C'){
					ans++;
					break;
				}
			}
			if(i-2>=0 && i-1>=0){	//插入到第三个 
				if(str[i-2]=='C' && str[i-1] == 'C' && str[i] == 'C'){
					ans++;
					break;
				}
			}
			if(i-3>=0 && i-2>=0 && i-1>=0){	//插入到第四个位置 
				if(str[i-3] == 'C' && str[i-2] == 'C' && str[i-1] == 'P'){
					ans++;
					break;
				}
			}
		}
		
		cout << ans << endl;
	}	
	
	return 0;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值