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’.
Codeforces Angry Students – 1287A solution in C/C++
#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; } }
Click to rate this post!
[Total: 5 Average: 4]