Cattle off topic B

Link
Source: Niuke.com

The title description
gives a string s containing only lowercase letters. You can operate it up to k times to make any lowercase letter become its adjacent lowercase letter (the absolute value of the ASCII code difference is 1), please find The longest possible equal subsequence (that is, find the longest subsequence after this string is modified at most k times, and it is necessary to ensure that each letter in this subsequence is equal).

Subsequence: Take any number of letters from the original string to form a new string in order

Input:
2 abcde
10 acesxd

Output
3
4

int string2(int k, string s) {
int n,i,j;
int book[220]={0};
int len=s.length();
for(i=0;i<len;i++)
book[s[i]]++;
int maxx=-1,sum,m;
n=k;
for(i=97;i<=122;i++)
{
sum=0;
m=n;
if(book[i]>0)
{
sum=0;
sum+=book[i];
j=i;
k=i;
while(m>0)
{
if(j>122&&k<97)
break;
j++;
if(j<=122)
{
if(book[j](j-i)<=m)
{
sum+=book[j];
m-=book[j]
(j-i);
}
else
{
sum+=(m/(j-i));
break;
}
}
k–;
if(k<97||book[k]==0)
continue;
if(book[k](i-k)<=m)
{
sum+=book[k];
m-=book[k]
(i-k);
}
else
{
sum+=(m/(i-k));
break;
}
}
if(maxx<sum)
maxx=sum;
}
}
return maxx;
}
};

int string2(int k, string s) {
// write code here
int i,j;
int a[3622];
int b[3345];
int len;
len=s.size();
for(i=0;i<len;i++)
{
a[i]=s[i]-‘a’;
}
int mx=-1;
for(i=0;i<26;i++)
{
for(j=0;j<len;j++)
b[j]=abs(a[j]-i);
sort(b,b+len);
// printf(“i=%d>>>\n”,i);
// for(j=0;j<len;j++)
// printf("%d “,b[j]);
// printf(”%>>\n");
int bu=k;
for(j=0;j<len;j++)
{
bu=bu-b[j];
if(bu<0)
break;
}
if(j>mx)
{
// printf(“i=%d>>>>>>>>>>>>\n”,j);
mx=j;
}
}
return mx;
}
};

Guess you like

Origin blog.csdn.net/Helinshan/article/details/110679556