传送门:

  [1]:BZOJ

  [2]:洛谷

•题解

  定义数组 a,b,c 分别表示 'J' , 'O' , 'I' 的前缀和;

  要想使区间 (L,R] 满足条件当且仅当 a[R]-a[L] = b[R]-b[L] = c[R]-c[L];

  那么,由 a[R]-a[L] = b[R]-b[L] ⇔ a[R]-b[R] = a[L]-b[L];

  同理,由 b[R]-b[L] = c[R]-c[L] ⇔ b[R]-c[R] = b[L]-c[L];

  提前预处理出 a,b,c 数组后;

  对于 i 位置,查找之前是否含有满足 ai-bi = aj-bj && bi-ci = bj-cj 的位置 j,并且 j 尽可能的小;

  如何高效的查找呢?

  使用 map<pair<int ,int > , int >;

  对于之前处理过的位置 j ,将 aj-bj 和 bj-cj 存入到 pair<int ,int > 中,每次查找是否存在 (ai-bi,bi-ci) 即可;

  如果存在,求解当前答案,反之,将其加入到map中;

•Code

 #include<bits/stdc++.h>
using namespace std;
#define pii pair<int ,int >
const int maxn=2e5+; int n;
char s[maxn];
int a[maxn];
int b[maxn];
int c[maxn];
map<pii ,int >f; int Solve()
{
f.clear();
f[pii(,)]=;///将(0,0)加入到f中
a[]=b[]=c[]=; int len=strlen(s+);
for(int i=;i <= len;++i)
{
a[i]=a[i-]+(s[i] == 'J');
b[i]=b[i-]+(s[i] == 'O');
c[i]=c[i-]+(s[i] == 'I');
} int ans=;
for(int i=;i <= len;++i)
{
pii tmp=pii(a[i]-b[i],b[i]-c[i]);
if(f.count(tmp))
ans=max(ans,i-f[tmp]);
else
f[tmp]=i;
}
return ans;
}
int main()
{
scanf("%d",&n);
scanf("%s",s+); printf("%d\n",Solve()); return ;
}

BZOJ 4236 "JOIOJI"(前缀和+map+pair)的更多相关文章

  1. bzoj 4236: JOIOJI【前缀和+map】

    设sj,so,si分别是J O I的个数前缀和,然后要求求最长(l,r)满足sj[r]-sj[l-1]==so[r]-so[l-1]==si[r]-si[l-1],化简一下就是满足so[r]-so[l ...

  2. BZOJ 4236: JOIOJI MAP

    4236: JOIOJI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.ph ...

  3. BZOJ 4236 JOIOJI(前缀和)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4236 [题目大意] 给出一个只包含三种字母的字符串,求出三种字母数量相等的最长子串 [ ...

  4. BZOJ 4236: JOIOJI map瞎搞

    分别记录J,O,I,的个数 cnt[char][i] 表示处理到第i位,char的个数 显然当且仅当 cnt[J][i] - cnt[O][i] == cnt[J][j-1] - cnt[O][j-1 ...

  5. BZOJ 4236: JOIOJI

    Description 给出一个字符串,只包含3个字母,询问最长的一个子串,3个字母出现次数相同. Sol map. 如果一个子串满足条件,那么它端点处的三个字母的个数两两差值都是一样的,直接存个状态 ...

  6. BZOJ 4236~4247 题解

    BZOJ 4236 JOIOJI f[i][0..2]表示前i个字符中′J′/′O′/′I′的个数 将二元组<f[i][0]−f[i][1],f[i][1]−f[i][2]>扔进map,记 ...

  7. Codeforces Round #378 (Div. 2) D. Kostya the Sculptor map+pair

    D. Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  8. CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用

    Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...

  9. 2018 ICPC 徐州网络预赛 Features Track (STL map pair)

    [传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...

随机推荐

  1. PHP 中的 curl 函数发送 Post 请求应该注意的几点

    public function http_request( $url, $post = '', $timeout = 5 ){ if( empty( $url ) ){return ;}$ch = c ...

  2. Ubuntu无法连接无线网

    shell里输入: su ifconfig wlan0 up 不行的话 rfkill block all rfkill unblock all ifconfig wlan0 up

  3. 【JZOJ4923】【NOIP2017提高组模拟12.17】巧克力狂欢

    题目描述 Alice和Bob有一棵树(无根.无向),在第i个点上有ai个巧克力.首先,两人个选择一个起点(不同的),获得点上的巧克力:接着两人轮流操作(Alice先),操作的定义是:在树上找一个两人都 ...

  4. python 正确地初始化对象

  5. SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作)

    SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作) 2012-12-21  未来决定...       http://www.ebama.net/thread-107643-1-1.html ...

  6. 2018-8-10-win10-uwp-如何创建修改保存位图

    title author date CreateTime categories win10 uwp 如何创建修改保存位图 lindexi 2018-08-10 19:16:50 +0800 2018- ...

  7. Python与Java异常类层级区别

  8. 手动实现一个form组件

    最近研究了一下element-ui,想着手动实现一下里面的form组件,贴个组件里面的代码 <el-form :model="ruleForm" status-icon :r ...

  9. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  10. AtCoder Beginner Contest 075 C bridge【图论求桥】

    AtCoder Beginner Contest 075 C bridge 桥就是指图中这样的边,删除它以后整个图不连通.本题就是求桥个数的裸题. dfn[u]指在dfs中搜索到u节点的次序值,low ...