zoukankan      html  css  js  c++  java
  • [JOISC2014]JOIOJI

    题目大意:
      给你一串仅包含'J''O''I'的字符串,问满足三种字符出现次数相等的最大字串是多少?

    思路:
      用map存一下出现次数前缀和两两之差出现的最早位置,每次看一下当前的两两之差最早的出现位置是多少。

     1 #include<map>
     2 #include<cstdio>
     3 #include<cctype>
     4 inline int getint() {
     5     register char ch;
     6     while(!isdigit(ch=getchar()));
     7     register int x=ch^'0';
     8     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
     9     return x;
    10 }
    11 inline char getalpha() {
    12     register char ch;
    13     while(!isalpha(ch=getchar()));
    14     return ch;
    15 }
    16 std::map<std::pair<int,int>,int> map;
    17 int main() {
    18     const int n=getint();
    19     int ans=0;
    20     map[std::make_pair(0,0)]=0;
    21     for(register int i=1,s[3]={0,0,0};i<=n;i++) {
    22         char ch=getalpha();
    23         if(ch=='J') s[0]++;
    24         if(ch=='O') s[1]++;
    25         if(ch=='I') s[2]++;
    26         std::pair<int,int> p=std::make_pair(s[1]-s[0],s[2]-s[0]);
    27         if(map.count(p)) {
    28             ans=std::max(ans,i-map[p]);
    29         } else {
    30             map[p]=i;
    31         }
    32     }
    33     printf("%d
    ",ans);
    34     return 0;
    35 }
  • 相关阅读:
    r语言
    pickle模块
    mongodb简介
    oracle
    mysql
    ceph
    迭代器
    python类
    encode&decode
    PHP 截取字符串中某两个字符(串)之间的字符串 考虑多个的情况
  • 原文地址:https://www.cnblogs.com/skylee03/p/8124783.html
Copyright © 2011-2022 走看看