fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main() {
  7. int n,k;
  8. cin>>n>>k;
  9. string s;
  10. cin>>s;
  11. vector<int> dp(n,1);
  12.  
  13. int ml=1;
  14. int mi=0;
  15.  
  16. for(int i=1;i<n;i++) {
  17. if(abs(s[i]-s[i-1])<=k) {
  18. dp[i]=dp[i-1]+1;
  19. } else {
  20. dp[i]=1;
  21.  
  22.  
  23. }
  24. if(dp[i]>ml) {
  25. ml=dp[i];
  26. mi=i;
  27. }
  28.  
  29.  
  30. }
  31. int si=mi-ml+1;
  32. cout<<s.substr(si,ml)<<endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5284KB
stdin
10 0 
hackerrank 
stdout
rr