Advertisement
tepyotin2

Feeding The Cow - Dec22

Jan 21st, 2024
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void printArray(int cnt, int n, string pos[]){
  5.     //cout << "print: "<< cnt << " n " <<  n << endl;
  6.     cout << cnt << endl;
  7.     for(int i = 0; i < n; i++){
  8.         //cout << " i: " << i << " pos" << pos[i] << endl;
  9.         if( pos[i].empty()){
  10.             cout << ".";
  11.         }else{
  12.             cout << pos[i] ;
  13.         }
  14.     }
  15.     cout << endl;
  16.     //cout << endl << "=====end=====" << endl;
  17. }
  18. int main(){
  19.     //ifstream fin("shuffle_bronze_dec17/2.in");
  20.     //ifstream fin("feeding.in");
  21.     //freopen("feeding.in", "r", stdin);
  22.     int t ;
  23.     cin >> t;
  24.    
  25.     //cout << t;
  26.     int n, k;
  27.     //cout << n << endl;
  28.     for(int i = 0 ; i < t;  i++){
  29.         cin >> n;
  30.         cin >> k;
  31.         string pos[n];
  32.        
  33.         string word;
  34.         cin >> word;
  35.        
  36.        
  37.         int cnt = 0;
  38.         for(int j = 0; j < n; j++){
  39.             string type;
  40.             //cin >> type;
  41.             type = word.substr(j,1);
  42.             int next = j + k;
  43.             if( next >= n){
  44.                 next = n-1;
  45.             }
  46.             int prev = j - k;
  47.             if(prev < 0){
  48.                 prev = 0;
  49.             }
  50.            
  51.             int max = 0;
  52.             bool found = false;
  53.            
  54.            
  55.             for(int k = next; k >= prev; k--){
  56.                 if(pos[k] == type){
  57.                     found = true;
  58.                     break;
  59.                 }else if(pos[k].empty()){
  60.                     //cnt++;
  61.                     //pos[k] = type;
  62.                     if(max == 0){
  63.                         max = k;
  64.                     }
  65.                     //break;
  66.                 }
  67.             }
  68.             if(!found){
  69.                 cnt++;
  70.                 pos[max] = type;
  71.             }
  72.             //cnt++;
  73.             //pos[j] = type;
  74.             //cout << "j: " << j << " type" << type << endl;
  75.         }
  76.        
  77.         printArray(cnt, n, pos);
  78.     }
  79.    
  80.  
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement