P3590 [POI2015] TRZ

有趣的结论题。

结论就是最长的满足条件的字串的左端点在最左边三个或者右端点在最右边三个

枚举即可。至于结论怎么来的?场上只能打表了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 1000005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
int suf[maxn][3];
char s[maxn];
int ans;
inline int solve(int l,int r) {
int x,y,z;
x=suf[r][0]-suf[l-1][0];
y=suf[r][1]-suf[l-1][1];
z=suf[r][2]-suf[l-1][2];
int sum=(x==0)+(y==0)+(z==0);
if (sum==2) return 1;
if (x==y||y==z||z==x) return 0;
return 1;
}
signed main(void){
// freopen("1.in","r",stdin);
int i,j,n;
read(n);
scanf("%s",s+1);
for (i=1;i<=n;i++) {
if (s[i]=='B') suf[i][0]=suf[i-1][0]+1;else suf[i][0]=suf[i-1][0];
if (s[i]=='C') suf[i][1]=suf[i-1][1]+1;else suf[i][1]=suf[i-1][1];
if (s[i]=='S') suf[i][2]=suf[i-1][2]+1;else suf[i][2]=suf[i-1][2];
}
for (i=1;i<=3&&i<=n;i++) {
for (j=n;j>=i;j--) if (solve(i,j)) {ans=max(ans,j-i+1);break;}
}
for (i=n;i>=n-2&&i>=1;i--) {
for (j=1;j<=i;j++) if (solve(j,i)) {ans=max(ans,i-j+1);break;}
}
printf("%d",ans);
return 0;
}