Tuesday, 26 August 2014

Problem analysis of Ponyo

PROBLEM: PONYO

Simply put, the problem asks you to find the longest subsequence of STR containing only R.
This can be done as given in the following pseudocode:

PSEUDOCODE:

STR = input()
i = 0
m = 0
c = 0
l = len(STR)
while i < l:
    if STR[i] == 'R':
        c++
    else
        c = 0
    if(m < c)
        m = c
    i = i + 1
print(m)

PROBLEM SETTER'S CODE IN C++:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
    int t;
    cin >> t;
    string str;
    while(t--) {
        cin >> str;
        int d = 0;
        int len = str.size();
        int cnt = 0;
        for(int i = 0; i < len; i++) {
            if(str[i] == 'R')
                cnt++;
            else
                cnt = 0;
            if(cnt > d)
                d = cnt;
        }
        cout << d << endl;
    }
    return 0;
}

No comments:

Post a Comment