跳到正文

题解

AT5158 [AGC037A] Dividing a String题解

zct_sky约 1 分钟
原文
文章信息与版本

文章信息

发布
2021年12月3日
收录
2026年10月14日
更新
2026年11月21日
篇幅
约 1 分钟
获赞
5

版本

  1. v1当前

题意:

给定一个字符串 SS,

将 SS 分成 kk 段,使相邻的两段不相同。

请你找出最大的 kk。

思路:

一道很简单的贪心题。

因为只要使相邻的两段不相同,所以只需将 SS 从头到尾遍历一遍,如果前一个字符串和后一个字符串不同,则单独提取出来,并把 ans+1ans+1 。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
string a,b,s;
int l,ans;
int main(){
	cin>>s;//输入 
	l=s.length();
	for(int i=0;i<l;i++){
		a+=s[i];
		if(a!=b){//如果前一个字符串和后一个字符串不同
			b=a;//则提取出来 
			ans++;
			a="";
		}
	}
	cout<<ans;//输出 
	return 0;
} 
原文