Sunday, November 20, 2016

URI Solution 1262 Multiple Reading in C++ language

URI Online Judge | 1262

Multiple Reading


By TopCoder*  USA
Timelimit: 1
In many computer systems, multiple processes can read from the same resource during the same clock cycle, but only a single process can write to the resource during a clock cycle. Reads and writes cannot be mixed during the same clock cycle. Given a history of the reads and writes that occurred during a particular computation as a String trace, and an int procs representing the number of processes used by the computation, calculate the minimum duration of the computation in clock cycles. The trace represents each read as an 'R' and each write as a 'W'.
For example, if trace is "RWWRRR" and procs is 3, then the minimum number of clock cycles is 4: one for the first read, one each for the two writes, and one for the last group of reads.

Input


The input contains several test cases. Each test case is composed by two lines. The first line has a string made out of 1 to 50 characters, where each can be either 'R' or 'W'. The second line contains an integer (1 ≤ ≤ 10), which represents the number of processes as a direct indicator of how many read operations can be performed simultaneously. The input stream ends in EOF.

Output

For each test case determine and print the minimum number of clock cycles required to run the given trace. For further reference see the examples below.
Sample InputSample Output
RWWRRR
3
RWWRRRR
3
WWWWW
5
RRRRRRRRRR
4
RWRRWWRWRWRRRWWRRRRWRRWRRWRRRRRRRRRWRWRWRRRRWRRRRR
4
4
5
5
3
30


URI Solution 1262 Multiple Reading in C++ language


#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    string s;
    int p, size, ciclos, count;
    while(cin >> s >> p)
    {
        size = s.size();
        ciclos = 0; count = 0;
        for (int i = 0; i < size; ++i)
        {
            if(s[i] == 'R'){
                if(count == 0)
                    ciclos++;
                count++;
                if(count == p)
                    count = 0;
            }else{
                ciclos++;
                count = 0;
            }
        }
        printf("%i\n", ciclos);
    }
    return 0;
}

Download the main C++ Code from Dropbox (If any add occur please click skip button)


Tags:
URI solution 1262, URI onlien Judge Solution 1262, URI Multiple reading problem solution, URI problem solution, URI 1262 code, URI 1262 code in C++


No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.