Guest User

Untitled

a guest
Jan 31st, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int l = 0;
  7. int lans = 0 , rans = 1000000000;
  8. int cur = 0;
  9. int k;
  10. int a[26];
  11. string s;
  12.  
  13. int main()
  14. {
  15.  
  16.     cin >> s;
  17.     cin >> k;
  18.  
  19.     for (int r = 0; r < s.length(); r++){
  20.         if (++a[s[r] - 'a'] == 1) cur++;
  21.  
  22.         while (cur > k){
  23.             if (--a[s[l] - 'a'] == 0) cur--;
  24.             l++;
  25.         }
  26.  
  27.         while (cur == k){
  28.             if (a[s[l] - 'a'] > 1){
  29.                 a[s[l] - 'a']--;
  30.                 l++;
  31.             }
  32.             else{
  33.                 break;
  34.             }
  35.         }
  36.  
  37.         if (cur == k){
  38.             if (rans - lans + 1 > r - l + 1){
  39.                 lans = l;
  40.                 rans = r;
  41.             }
  42.         }
  43.     }
  44.  
  45.     if (rans == 1000000000){
  46.         cout << -1;
  47.         return 0;
  48.     }
  49.  
  50.     cout << rans - lans + 1;
  51.  
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment