竞赛讨论区 > B题求助
头像
errormakers
编辑于 2021-11-27 19:44
+ 关注

B题求助

#include <bits/stdc++.h>
using namespace std;
int lll[26];
int main()
{
    int t,k,num=0,ans=50***00;
    string s;
    int d;
    cin>>t;
    for(int p=0;p<t;p++)
    {
        cin>>s>>k;
        ans=50***00;
        memset(lll,0,sizeof lll);
        int l=s.length();
        for(int i=0;i<l;i++)
        {
            lll[s[i]-'a']++;
            if(lll[s[i]-'a']>=k)
            {
                d=lll[s[i]-'a']-k;
                for (int j = 0; j < 26; j++) {
                    if (s[j] == s[i]) {
                        num = abs(i - j+1);
                        ans=min(num,ans);
                        if(d==0)
                            break;
                        else d--;
                    }
                }
            }
        }
        if(ans==50***00)
            cout<<"-1\n";
        else
            cout<<ans<<endl;

    }

}

全部评论

(0) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐