siyecao8963 @ 2023-11-01 00:16:55
#include <stdio.h>
#define maxn 1000010
int main()
{
char ch;
int i,max,b[maxn];
i=0;
for (int j=1;j<=maxn;j++)
b[j]=0;
while (1){
scanf("%c",&ch);
if (ch!='G'&&ch!='R') break;
i++;
if (ch=='G') b[i]=1;
else if (ch=='R') b[i]=-1;
b[i]+=b[i-1];
}
max=0;
//for (int j=1;j<=i;j++) printf("%d ",b[j]);
//printf("\n");
for (int j=0;j<i;j++){
if (max>i-j) break;
for (int k=i;k>j;k--)
if (b[k]==b[j]&&k-j>max) {max=k-j;break;}
}
printf("%d\n",max);
}
#include <stdio.h>
#include <string.h>
#define maxn 100010
int main()
{
char ch;
int n,max,b[maxn];
char s[100010];
for (int j=1;j<=maxn;j++)
b[j]=0;
scanf("%s",s);
n=strlen(s);
for (int i=1;i<=n;i++){
if (s[i-1]=='G') b[i]=1;
else b[i]=-1;
b[i]+=b[i-1];
}
max=0;
//for (int j=1;j<=i;j++) printf("%d ",b[j]);
//printf("\n");
for (int j=0;j<n;j++){
if (max>n-j) break;
for (int k=n;k>j;k--)
if (b[k]==b[j]&&k-j>max) {max=k-j;break;}
}
printf("%d\n",max);
}