3 条题解

  • 2
    @ 2024-5-17 21:57:01
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        string s;
        cin>>s;
        long t=0,e=0,c=0,m=0;
        while(e<s.length())
        {
            char ch=s[e];
            for(long i=t;i<e;i++)
            {
                if(s[i]==ch)
                {
                    t=i+1;
                    c=e-t;
                    break;
                }
            }
            e++;
            c++;
            m=max(m,c);
        }
        cout<<m;
        return 0;
    }
    
    • 1
      @ 2024-1-5 16:08:16
      提示

      考虑使用滑动窗口的方式,右指针先右移一步,然后判断右指针所处位置的字符是否出现过,若重复出现了就需要不断移动左指针,确保区间内不存在重复字符,在此期间记录符合要求的最大区间即可。

      • 0
        @ 2024-5-18 17:39:45
        //最简单的方法
        
        #include <bits/stdc++.h>
        using namespace std;
        int l,r,cnt[1001];
        string s;
        int main()
        {
        // freopen("filename.in", "r", stdin);
        // freopen("filename.out", "w", stdout);
        cin>>s;
        l=1,r=1;
        int ans=0;
        for(;r<=s.size();r++)
        {
        cnt[s[r-1]-'a']++;
        while(cnt[s[r-1]-'a']>1)cnt[s[l-1]-'a']--,l++;
        ans=max(ans,r-l+1);
        }
        cout<<ans;
        return 0;
        }
        
        • 1

        信息

        ID
        632
        时间
        1000ms
        内存
        256MiB
        难度
        4
        标签
        (无)
        递交数
        157
        已通过
        76
        上传者