C. K-Dominant Character

本文介绍了一种算法,用于寻找给定字符串中最短长度的子串,确保至少包含一种k-主导字符。通过分析字符间的最大间隔来确定这一长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.

You have to find minimum k such that there exists at least one k-dominant character.

Input

The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

Output

Print one number — the minimum value of k such that there exists at least one k-dominant character.

Examples
Input
abacaba
Output
2
Input
zzzzz
Output
1
Input
abcde
Output
3

给一个字符串,找最短每几个字符都会出现一个字符X(X不确定)。

思路:找到每个同一字符相距最远的长度d,则此字符为每d个长度的字符串都出现的字符,再找最小的d。需要注意的是求得d还要考虑第一个字符距离开始0的位置,和最后一个此字符距离结束n的位置。情况考虑不全wrong好几次

#include<bits/stdc++.h>
using namespace std;
char s[100010];
map<char,int>q;
map<char,int>m;
map<char,int>w;
int main()
{
    scanf("%s",s+1);
    int l=strlen(s+1);
    q.clear();
    m.clear();
    w.clear();
    if(l==1)
    {
        printf("1\n");
       return 0;
    }
    for(int i=l;i>=0;i--)
        w[s[i]]=i;
    for(int i=1;i<=l;i++)
    {
        if(m[s[i]]!=0)
        q[s[i]]=max(q[s[i]],i-m[s[i]]);
        m[s[i]]=i;
    }
    int ans=100000000;
    for(int i=1;i<=l;i++)
    {
        if(q[s[i]]!=0)
         {
          q[s[i]]=max(q[s[i]],l-m[s[i]]+1);
          q[s[i]]=max(q[s[i]],w[s[i]]);
          if(q[s[i]]<ans)
          ans=q[s[i]];
         }
    }
    int p=l/2+1;
    if(ans==100000000)
    ans=l/2+(l/2==0?0:1);
    printf("%d\n",min(p,ans));
}



利用matlab识别[-37.6141564375877 -44.8934318146598 -68.5395102231449 -105.998636800599 -39.0202173875387 -15.0676705979633 -70.0289155856468 -89.6183719712618 -31.4998736017068 -65.6706070347323 -52.2954712582728 -25.0007417656538 -62.0175753680824 -52.4505190780410 -36.7998214080831 -66.8240803995901 -89.7310248077318 -89.4476631841424 -95.0631484078666 -45.4266150893948 -71.4268623418806 -87.7389305484529 -90.7689607725678 -89.4482765744236 -63.4873672376004 -50.5822037813285 -89.0051497735307 -104.630351899095 -71.8133034594315 -87.1409454746534 -101.322624957044 -96.8859043636271 -107.236700645133 -102.635430504787 -85.8695232644000 -88.0600512107977 -91.2951358823337 -74.5224644056697 -81.4289479344271 -92.8546062893397 -64.3662613256191 -60.7190970833584 -3.55577647563955 -44.7515582179228 -55.2621821199571 -58.1191843564058 -64.7337352687441 -58.0577833197574 -46.8047623917448 -45.1175093264491 -46.4333171948036 -57.5279718954082 5.02150694070810 -23.4266564799937 -0.389119636882192 10.3895229106278 3.83098733011624 -1.66067566291584 7.99355669801910 19.6746491807298 26.0543573655568 15.3122579508032 13.5242208685403 2.98885682332441 13.4193364621863 2.55985415353877 60.7869486491908 -7.19843777126744 50.0025300398218 -19.2306656104956 84.7794322602891 45.4102510636858 116.883089912579 102.259133619137 14.0774708419592 28.4068229352914 27.8204958611579 31.4538966828311 93.2036965898183 -2.61208839117787 -38.6618143537309 -23.3092017890169 -15.6724244605204 -37.2371241010961 20.3178013223489 49.1877327947727 -16.3352241625370 44.4518673391411 34.8328106589607 -3.39617598124692 4.22761052220510 -127.033670095793 -106.268209096416 -86.7501347324323 -44.1581155090221 -89.0199577753159 -75.9603939084827 -78.8226974792923 -103.391357116246 -115.267742416180 -45.1877231021113 -89.3961504838651 -51.5825019761869 -55.0672415959877 -62.6021257843332 -63.5984516565330 -65.8130844153139 -58.2838155448689 -93.8797357975003 -73.5215320794161 -116.822323814052 -62.761410181]的特征
最新发布
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值