题解 | #String of CCPC#

String of CCPC

https://ac.nowcoder.com/acm/problem/14367

思路

先找CCPC的个数->找CCC、CPC、CCC的个数并且避开CCPC,我们容易发现每次答案最多加1。 验证一下:CCPCPC以及CCPCPCCPCPC

代码

//#pragma GCC optimize("Ofast", "inline", "-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}

int t,n,ans;
string str;

signed main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
//  freopen("in.cpp","r",stdin);
//  freopen("out.cpp","w",stdout);
	cin>>t;
	while(t--){
		cin>>n;
		cin>>str;
		ans=0;
		for(int i=0;i<str.length();i++){
			if(str.substr(i,4)=="CCPC") ans++;
		}
		for(int i=0;i<str.length();i++){
			if(str.substr(i,4)=="CCPC"){i+=2;continue;}
			string s=str.substr(i,3);
			if(s=="CCC"&&str.substr(i+1,4)!="CCPC"){ans++;break;}
			else if(s=="CPC"||s=="CCP"){ans++;break;}
		}
		cout<<ans<<"\n";
	} 
	return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
牛客92804383...:在他心里你已经是他的员工了
点赞 评论 收藏
分享
哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务