题意
一个字符串,问最多可以将其分割出来几部分可以整除3的数字?
题解
首先,%3=0的单个数字成为一部分,其他的数字均对3取余,相邻12,21,111,222一定能成为一个整除3的数字。
AC代码1:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 120005;
const int INF = 0x3f3f3f3f;
typedef long long LL;
string str;
int main()
{
cin >> str;
int ans = 0;
for (int i = 0; i < str.size(); i++)
{
int t = str[i]-'0';
if (t%3==0){ str[i] = '#', ans++; }
else str[i] = t%3+'0';
if (i >= 2 && str[i] == '1' && str[i-1] == '1' && str[i-2] == '1') ans++, str[i] = str[i-1] = str[i-2] = '#';
if (i >= 2 && str[i] == '2' && str[i-1] == '2' && str[i-2] == '2') ans++, str[i] = str[i-1] = str[i-2] = '#';
if (i >= 1 && str[i] == '2' && str[i-1] == '1') ans++, str[i] = str[i-1] = '#';
if (i >= 1 && str[i] == '1' && str[i-1] == '2') ans++, str[i] = str[i-1] = '#';
}
printf("%d\n", ans);
return 0;
}
/*
201920181
10232321
101112211113
123
221
112
222
111
*/
AC代码2:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 120005;
const int INF = 0x3f3f3f3f;
typedef long long LL;
string str;
int main()
{
cin >> str;
int ans = 0, cnt = 0, x = 0;
for (int i = 0; i < str.size(); i++)
{
int t = str[i]-'0';
cnt++; x += t%3;
if (t%3==0 || x%3 == 0 || cnt == 3)
{
ans++;
cnt = x = 0;
}
}
printf("%d\n", ans);
return 0;
}
/*
201920181
10232321
101112211113
123
221
112
222
111
*/