牛客 排数字(简单找规律)

题目链接:点击这里

在这里插入图片描述
在这里插入图片描述

1 1 6 6 以外的不需要考虑。

要让 616 616 子串最多一定是 61616 61616… ,这样后面的串可以用前面的 6 6 ,数量为 m i n ( c n t 6 1 ,   c n t 1 ) min⁡(cnt6−1,\ cnt1)

时间复杂度 O ( S ) O(∣S∣)

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 200010;
int a[maxn];
int n;
char s[maxn];

int main()
{
	scanf("%d", &n);
	scanf("%s", s);
	
	int cnt1 = 0, cnt6 = 0;
	for(int i = 0; i < n; ++i)
	{
		if(s[i]=='1')    cnt1++;
		else if(s[i]=='6')    cnt6++;
	}
	
	if(cnt1==0||cnt6==0||cnt6==1)
	{
		printf("0\n");
		return 0;
	}
	
	if(cnt1<cnt6)	printf("%d\n", cnt1);
	else if(cnt1==cnt6)	printf("%d\n", cnt6-1);
	else if(cnt6<cnt1)	printf("%d\n", cnt6-1);
	
	return 0;
}
发布了727 篇原创文章 · 获赞 111 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104201627