根据每个字符的模3性,分类讨论即可,注意特判必须删第一个数字的情况。
#include<bits/stdc++.h> #define int long long #define double long double #define x first #define y second using namespace std; typedef long long LL; typedef long long ll; typedef pair<int,int> PII; const int N=3e5+10; const int M=1e3+10; int mod=1e9+7; int a[N]; void solve(){ string s;cin>>s; vector<int>v(4); int sum=0; for(auto it:s){ int x=it-'0'; sum+=x; v[x%3]++; } int sp=s.size(); sum%=3; // cout<<sum<<"\n"; if(sum==0){ if(v[0]==s.size()) cout<<v[0]-1<<"\n"; else cout<<v[0]<<"\n"; return ; } if(v[sum]){ v[sum]--; sp--; if(v[sum]==0&&sum==(s[0]-'0')%3){ int pos=1; while(pos<s.size()&&s[pos]=='0') v[0]--,pos++,sp--; } if(v[0]==sp) cout<<v[0]<<"\n"; else cout<<v[0]+1<<"\n"; }else cout<<0<<"\n"; } signed main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int _; _=1; cin>>_; while(_--){ solve(); } }