fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, k;
  6. cin>>n>>k;
  7. string s;
  8. cin>>s;
  9. vector<int> dp(n, 1);
  10. int maxLength=1, maxIndex=0;
  11. // dp[1]=1;
  12. for(int i=1;i<n;i++){
  13. if(abs(s[i]-s[i-1])<=k)
  14. dp[i]=dp[i-1]+1;
  15. else
  16. dp[i]=1;
  17.  
  18. if(dp[i]>maxLength){
  19. maxLength=dp[i];
  20. maxIndex=i;
  21. }
  22. }
  23. int startIndex = maxIndex - maxLength + 1;
  24. cout << s.substr(startIndex, maxLength) << endl;
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5320KB
stdin
10 2
abaabcadef
stdout
abaabca