BZOJ 4236 set乱搞
思路:
取个差 在set里面找 更新
(这个用map更好吧 但是我不会……)
//By SiriusRen
#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,sum[200050][3],ans;
char ch[200050];
struct Node{int x,y,rec;}jy;
struct cmp{
bool operator ()(Node a,Node b){
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
};
set<Node,cmp>s;
set<Node,cmp>::iterator it;
int main(){
scanf("%d%s",&n,ch+1);
s.insert(jy);
for(int i=1;i<=n;i++){
sum[i][0]=sum[i-1][0]+(ch[i]=='J');
sum[i][1]=sum[i-1][1]+(ch[i]=='O');
sum[i][2]=sum[i-1][2]+(ch[i]=='I');
jy.x=sum[i][1]-sum[i][0],jy.y=sum[i][2]-sum[i][1];
jy.rec=i,it=s.find(jy);
if(it==s.end())s.insert(jy);
else ans=max(ans,i-it->rec);
}
printf("%d\n",ans);
}