Advertisement
SuitNdtie

Policemen Catch Thieves

May 29th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int k;
  6.     scanf("%d",&k);
  7.     char str[1010];
  8.     scanf(" %s",str);
  9.    
  10.     int ans = 0;
  11.     for(int i = 0 ; str[i] != '\0' ; i ++){
  12.         if(str[i] == 'P'){
  13.             int maxlen = 0;
  14.             int maxpos = -1;
  15.             for(int j = i - 1 ; j >= i - k && j >= 0 ; j --){
  16.                 if(str[j] != 'T')continue;
  17.                 if(i - j > maxlen){
  18.                     maxlen = i - j;
  19.                     maxpos = j;
  20.                 }
  21.             }
  22.             if(maxpos == -1){
  23.                 for(int j = i + 1 ; j <= i + k && str[j] != '\0' ; j ++){
  24.                     if(str[j] != 'T')continue;
  25.                     if(j - i > maxlen){
  26.                         maxlen = j - i;
  27.                         maxpos = j;
  28.                     }
  29.                 }
  30.                
  31.             }
  32.            
  33.            
  34.             if(maxpos != -1){
  35.                 ans ++;
  36.                 str[maxpos] = 't';
  37.             }
  38.         }
  39.     }
  40.     printf("%d",ans);
  41.     return 0;
  42. }
  43. /*
  44.  
  45. PTPP
  46. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement