# Positive solution

#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const ll N=1e6+10;

ll n;
char arr[N];

void solve()
{
scanf("%lld",&n);
scanf("%s",arr+1);
ll cont=min(n,7LL);
for(ll i=2; i<=cont; i++)
{
for(ll j=i; j<=n; j++)
{
ll cnt= {0,0,0};
for(ll k=j-i+1; k<=j; k++) cnt[arr[k]-'a']++;
if(cnt>cnt&&cnt>cnt)
{
printf("%lld\n",i);
return;
}
}
}
puts("-1");
}

int main()
{
ll t;
scanf("%lld",&t);
while(t--)
solve();
return 0;
}


# Problem solving

## 00:37:29 Wrong answer on pretest 2

The first two questions are in excellent condition, and the problem-solving speed is expected to be less than 1500.
The third question continued the state of the first two questions and was keenly aware that it was a conclusion question. However, considering the state of the first two questions, I had confidence in myself, so I didn't check the grammar and verify the conclusion.

#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const ll N=1e3+10;

ll n;
char arr[N];

void solve()
{
scanf("%lld",&n);
scanf("%s",arr+1);
for(ll i=2;i<=n;i++)
{
if(arr[i]=='a'&&arr[i-1]=='a')
{
puts("2");
return;
}
}
for(ll i=3;i<=n;i++)
{
ll cnt=0;
for(ll j=i-2;j<=i;j++) cnt+=(arr[j]=='a');
if(cnt>=2)
{
puts("3");
return;
}
}
puts("-1");
}

int main()
{
ll t;
scanf("%lld",&t);
while(t--)
solve();
return 0;
}


## 00:47:33 Wrong answer on pretest 2

The second attempt was the closest to the positive solution, but it was obvious that there were not many questions, and I was still worried that the violence would timeout. But I was lucky to get a 7 (probably because 7 is my lucky number). However, a conclusion was drawn at that time:
a > b , a > c ⇒ a + a > b + c , a + b + c = l e n ⇒ a + a > l e n − a ⇒ a × 3 > l e n a>b,a>c \\ \Rightarrow a+a>b+c,a+b+c=len \\ \Rightarrow a+a>len-a \\ \Rightarrow a\times 3>len a>b,a>c⇒a+a>b+c,a+b+c=len⇒a+a>len−a⇒a×3>len
So I think a × 3 > l e n ⇒ a > b , a > c a\times 3>len\Rightarrow a>b,a>c a×3>len⇒a>b,a>c.
This conclusion has not been verified. In fact, abba can.
And no syntax check.

#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const ll N=1e3+10;

ll n;
char arr[N];

void solve()
{
scanf("%lld",&n);
scanf("%s",arr+1);
ll cont=min(n,7LL);
for(ll i=2;i<=n;i++)
{
if(arr[i]=='a'&&arr[i-1]=='a')
{
puts("2");
return;
}
}
for(ll i=3;i<=cont;i++)
{
for(ll j=i;j<=n;j++)
{
ll cnt=0;
for(ll k=j-i+1;k<=j;k++) cnt+=(arr[k]=='a');
if(cnt*3>i)
{
printf("%lld\n",i);
return;
}
}
}
puts("-1");
}

int main()
{
ll t;
scanf("%lld",&t);
while(t--)
solve();
return 0;
}


## 01:07:25 Wrong answer on pretest 2

The idea is completely wrong (probably).
After that, I won't consider the previous idea. (it's normal. It's wa2 hard not to change your mind twice.)
It's not very good at this time.
According to the nature of the substring, think about whether it is dynamic programming, and then think about the contribution of adding a letter to the answer
Then a flash of light, this is similar to the largest substring and practice!
However, proving this conclusion was very complex. At that time, the idea was not clear and could not be proved, but it was written in a mask. Naturally, it was wrong.
Not verified, not checked

#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const ll N=1e3+10;

ll n;
char arr[N];

void solve()
{
scanf("%lld",&n);
scanf("%s",arr+1);
ll p=1;
ll cnta=0;
ll cntb=0;
ll cntc=0;
ll ans=n+1;
for(ll i=1;i<=n;i++)
{
if(arr[i]=='a') cnta++;
if(arr[i]=='b') cntb++;
if(arr[i]=='c') cntc++;
if(cnta>cntb&&cnta>cntc&&cnta+cntb+cntc>=2)
{
ans=min(ans,cnta+cntb+cntc);
cntb=cntc=0;
cnta=1;
}
if(cnta<cntb&&cnta<cntc||cnta==0) cnta=cntb=cntc=0;
}
if(ans==n+1) puts("-1");
else printf("%lld\n",ans);
}

int main()
{
ll t;
scanf("%lld",&t);
while(t--)
solve();
return 0;
}


## 01:17:33 Wrong answer on pretest 2

The "conclusion" was not easy to get "very clear", but it was still
Not verified
not checked
(after the game, it was found that there were more than 2000 groups of test samples, but it didn't show that this conclusion was a positive solution)
It's not in good condition. There's no code wind at all.

#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const ll N=1e3+10;

ll n;
char arr[N];

void solve()
{
scanf("%lld",&n);
scanf("%s",arr+1);
if(n==2)
{
if(arr=='a'&&arr=='a')
puts("2");
else
puts("-1");
return;
}
ll p=1;
ll cnta=0;
ll cntb=0;
ll cntc=0;
ll ans=n+1;
for(ll i=1; i<=n; i++)
{
if(arr[i]=='a') cnta++;
if(arr[i]=='b') cntb++;
if(arr[i]=='c') cntc++;
if(cnta>cntb&&cnta>cntc&&cnta+cntb+cntc>=2)
{
ans=min(ans,cnta+cntb+cntc);
cntb=cntc=0;
cnta=1;
}
if(cnta<cntb||cnta<cntc) cnta=cntb=cntc=0;
}
if(ans==n+1) puts("-1");
else printf("%lld\n",ans);
}

int main()
{
ll t;
scanf("%lld",&t);
while(t--)
solve();
return 0;
}


## 01:31:48 Accepted

Finally, I started to create data. I'm still thinking about circular nodes.
Suddenly found that the length of 6 and may become the answer string (abcbac) plus a letter can not become a possible answer, or it can not be constructed, or it can not become the answer. It was really good luck. Suddenly I thought of the original violence. Suddenly I didn't need the original conclusion. Suddenly I found:
const ll N=1e3+10;
const ll N=1e3+10;
const ll N=1e3+10;
Yes, but it's really bad. I didn't continue to look at question D.

#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const ll N=1e6+10;

ll n;
char arr[N];

void solve()
{
scanf("%lld",&n);
scanf("%s",arr+1);
ll cont=min(n,7LL);
for(ll i=2; i<=n; i++)
{
if(arr[i]=='a'&&arr[i-1]=='a')
{
puts("2");
return;
}
}
for(ll i=3; i<=cont; i++)
{
for(ll j=i; j<=n; j++)
{
ll cnta=0;
ll cntb=0;
ll cntc=0;
for(ll k=j-i+1; k<=j; k++)
{
cnta+=(arr[k]=='a');
cntb+=(arr[k]=='b');
cntc+=(arr[k]=='c');
}
if(cnta>cntb&&cnta>cntc)
{
printf("%lld\n",i);
return;
}
}
}
puts("-1");
}

int main()
{
ll t;
scanf("%lld",&t);
while(t--)
solve();
return 0;
}


# summary

This still needs to be summarized?
Check for syntax errors! Create data!
Check for syntax errors! Create data!
Check for syntax errors! Create data!
Check for syntax errors!
Check for syntax errors!
Check for syntax errors!

Keywords: Simulation

Added by project3 on Fri, 12 Nov 2021 20:51:58 +0200