BZOJ 4236: JOIOJI

Description

给出一个字符串,只包含3个字母,询问最长的一个子串,3个字母出现次数相同.

Sol

map.

如果一个子串满足条件,那么它端点处的三个字母的个数两两差值都是一样的,直接存个状态强行上map.

注意要加入一个初始状态.

Code

#include<cstdio>
#include<map>
#include<iostream>
using namespace std;
 
const int N = 200005;
 
struct S{ int a[3]; }g[N];
bool operator < (const S &x,const S &y){ return x.a[0]==y.a[0] ? (x.a[1] == y.a[1] ? x.a[2] < y.a[2] : x.a[1] < y.a[1]) : x.a[0] < y.a[0]; }
int n,ans,s[N];
map<S,int> mp;
 
inline int idx(char ch){ if(ch == 'J') return 0;else if(ch == 'O') return 1;else return 2; }
int main(){
    scanf("%d",&n);
    char ch=getchar();while(ch>'Z' || ch<'A') ch=getchar();
    for(int i=1;i<=n;i++) s[i]=idx(ch),ch=getchar(),g[i]=g[i-1],g[i].a[s[i]]++;
    S tmp;tmp.a[0]=tmp.a[1]=tmp.a[2]=0;
    mp[tmp]=0;
    for(int i=1;i<=n;i++){
        S tmp;
        tmp.a[0] = g[i].a[0]-g[i].a[1],tmp.a[1] = g[i].a[0]-g[i].a[2],tmp.a[2] = g[i].a[1]-g[i].a[2];
        if(mp.count(tmp)) ans=max(ans,i-mp[tmp]);
        else mp[tmp]=i;
    }return cout<<ans<<endl,0;
}

  

转载于:https://www.cnblogs.com/beiyuoi/p/6001544.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值