题目
一串字符串只含a和b,像a∗aa*aa∗a或b∗bb*bb∗b,∗*∗指匹配的字符,可以缩成∗*∗,求缩减后字符串的长度。
分析
模拟,当两个字符间只间隔一个字符,判断字符串的长度的奇偶性即可。
代码
#include <cstdio>
#include <cstring>
using namespace std;
char s[256];
int main(){
scanf("%s",s);
int len=strlen(s);
int ans=len;
for(int i=0;i+2<len;i++)
if(s[i]==s[i+2])
if(len%2) ans=1;
else ans=2;
printf("%d\n",ans);
return 0;
}
本文介绍了一种针对特定形式的字符串(仅包含'a'和'b')的缩减算法,通过模拟方式来确定缩减后的字符串长度。该算法关注于字符间的相对位置,并依据原始字符串长度的奇偶性来决定最终结果。
366

被折叠的 条评论
为什么被折叠?



