codeforces – Learn2Code https://learn2code.musitrature.com Learn to code : A programmers resource Mon, 13 Apr 2020 13:30:52 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.4 https://learn2code.musitrature.com/wp-content/uploads/2020/03/favicon-2.ico codeforces – Learn2Code https://learn2code.musitrature.com 32 32 174224216 Codeforces Angry Students – 1287A solution https://learn2code.musitrature.com/codeforces-angry-students-1287a-solution/?utm_source=rss&utm_medium=rss&utm_campaign=codeforces-angry-students-1287a-solution Sun, 29 Mar 2020 09:43:46 +0000 https://learn2code.musitrature.com/?p=308 Read More »Codeforces Angry Students – 1287A solution]]> This problem has denoted a row of students by a string. Where the string contains 2 char ‘A’ and ‘P’ denoting Angry and patients students respectively.

Now, the main point every angry student in a row throws a snowball every minute to its next student. Simply put, if a row is like this :

As mentioned before each minute any angry students ‘A’ will throw snow to the next student and if it was patient ‘P‘, will make him/her angry.e.g. ‘P’ to ‘A’. Exactly like this:

1st minute:

2nd minute:

3rd minute:

After 3rd minute:

As you can see we have to find the substring containing the longest sequence of ‘P’ because the longest consecutive ‘P’ can be changed to a sequence of ‘A’.But you can do that only if:

  • The string must have ‘P’ and ‘A’ both characters.
  • The longest sequence of ‘P’ starts with ‘A’. So, each iteration we can change the next ‘P’ to ‘A’.
#include<bits/stdc++.h>
using namespace std;
int main() {
    int tc,n,cnt,tmp_cnt;
    cin >> tc;
    while(tc--) {
        cnt = tmp_cnt = 0;
        cin >> n;
        char stu_row[n];
        cin >> stu_row;
        for(int i = 0; i<strlen(stu_row); i++) {
            if(stu_row[i] == 'A') {
                for(int j = i+1; j<=strlen(stu_row); j++) {
                    if(stu_row[j] == 'P')
                        ++tmp_cnt;
                    else if(stu_row[j] == 'A') {
                        break;
                    }
                }
                if(tmp_cnt > cnt) {
                    cnt = tmp_cnt;
                }
                tmp_cnt = 0;
            }
        }
        cout << cnt << endl;
    }
}

]]>
308
Codeforces 1220 – A. Cards solution https://learn2code.musitrature.com/codeforces-1220-a-cards-solution/?utm_source=rss&utm_medium=rss&utm_campaign=codeforces-1220-a-cards-solution Wed, 25 Mar 2020 09:51:36 +0000 https://learn2code.musitrature.com/?p=302 Read More »Codeforces 1220 – A. Cards solution]]>

This problem is very simple. First You have to input an integer n. Then input a randomized string of n consisting of these five English lowercase letters: ‘z’, ‘e’, ‘r’, ‘o’ and ‘n’.

Now the problem said you have to sort/restore the randomized/shuffled string and It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either “zero” which corresponds to the digit 0 or “one” which corresponds to the digit 1.

Here is where the problem gets tricky. If you think hard and try to find each “one” and “zero”s from the rearranged string you may need to make a complicated code and possibly a timeout error.

Instead, we can solve this pretty easily. If you analyse carefully: Out of the five characters that formed the string, between the two words “zero” and “one” z,r are unique to the word “zero” and “n” is unique to the word “one” and occurs only once.

So, If we count the number of “z” or “r” we can get the total number of “zero” in the string.
Similarly, If we count “n” we will get the total occurrence of “one”.

Now that we have found the total number of “one” and “zeros” we have to print the maximum binary number possible of them. Which is easy. Print all the ‘1’ first then “0”

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,one = 0, zero = 0;
    cin >> n;
    char str[n];
    cin >> str;
    for(int i = 0; i<strlen(str); i++) {
        if(str[i] == 'z')
            ++zero;
        else if(str[i] == 'n')
            ++one;
    }
    while(one--) cout << "1 ";
    while(zero--) cout << "0 ";
}

]]>
302
Codeforces 266b solution: Queue at the School https://learn2code.musitrature.com/codeforces-266b-solution-queue-at-school/?utm_source=rss&utm_medium=rss&utm_campaign=codeforces-266b-solution-queue-at-school Sat, 14 Mar 2020 09:32:32 +0000 http://learn2code.musitrature.com/?p=210 Read More »Codeforces 266b solution: Queue at the School]]> problem link

This problem’s main point is each second a boy will let every girl swap positions if a girl is behind the boy. This happens for all the girls and boys who meet the same condition.

5 1

BGBGB

Here, if you look at sample input you will see a single string containing only ‘B’ or ‘G’ letters denoting boy and girl respectively, where the sequence of the whole string represents the line containing boys and girls int the line/queue at a certain time.

The first integer represents the number of children ( in this case string size ) and the 2nd integer represents seconds that will pass.

  • First, we will take two integers as asked in the problem. (number of children and passable time).
  •  After inputting them we will declare a string whose length will be the 1st inputted integer to create the line of students as a string.
  • Then, we will call a loop until we pass all iterations of the second Input integer so that we can do operations of swapping boys and girls for the given seconds. (line 8 of code )
  • Now we will loop through all the index of the input string ( boys, girls queue representation ) to check if current_index = ‘B’ and next_index = ‘G’.
  • If the previous comparison is correct we will swap current_index and next_index values. ( Boys exchanging position with girls if the girl is behind ). (line 10 – 12 of code)
  • Then increment the loop iterator by 2 because we had done work with two indexes and we don,t need to do anything u til next second. (line 13 of code)
  • Else if the comparison didn’t match we will increment the loop iterator only by one so we can check the next index.
  • After the looping process is done just print the string to show the changed output.


#include<bits/stdc++.h>
int main() {
int x,y;
scanf("%d%d",&x,&y);
char str[x],tmp;
scanf(" %s",str);
int len = strlen(str);
while(y--) {
   for(int i = 0; i<len-1;) {
      if(str[i]=='B' && str[i+1]=='G') {
         tmp = str[i];
         str[i] = str[i+1];
         str[i+1] = tmp;
         i+=2;
      }else{
         i++;
          }
       }
    }
printf("%s\n",str);
return 0;
}
]]>
210