SRM 147 1A2A 2013.12.2
DIV 1
350
Problem Statement
There are numMales males and numFemalesfemales arranged in a circle. Starting from a given point, you count clockwiseand remove the K'th person from the circle (where K=1 is the person at thecurrent point, K=2 is the next person in the clockwise direction, etc...).After removing that person, the next person in the clockwise direction becomesthe new starting point. After repeating this procedure numFemales times, thereare no females left in the circle.
Given numMales, numFemales and K, your taskis to return what the initial arrangement of people in the circle must havebeen, starting from the starting point and in clockwise order.
For example, if there are 5 males and 3females and you remove every second person, your return String will be"MFMFMFMM".
Definition
Class:
PeopleCircle
Method:
order
Parameters:
int, int, int
Returns:
string
Method signature:
string order(int numMales, int numFemales,int K)
(be sure your method is public)
Constraints
-
numMales is between 0 and 25 inclusive
-
numFemales is between 0 and 25 inclusive
-
K is between 1 and 1000 inclusive
Examples
0)
5
3
2
Returns: "MFMFMFMM"
Return "MFMFMFMM". On the firstround you remove the second person - "M_MFMFMM". Your new circlelooks like "MFMFMMM" from your new starting point. Then you removethe second person again etc.
1)
7
3
1
Returns: "FFFMMMMMMM"
Starting from the starting point you removethe first person, then you continue and remove the next first person etc.Clearly, all the females are located at the beginning. Hence return"FFFMMMMMMM"
2)
25
25
1000
Returns:"MMMMMFFFFFFMFMFMMMFFMFFFFFFFFFMMMMMMMFFMFMMMFMFMMF"
3)
5
5
3
Returns: "MFFMMFFMFM"
Here we mark the removed people with '_',and the starting position with lower-case:
Number of | People Remaining
Rounds | (in initial order)
---------------+-----------------
0 | mFFMMFFMFM
1 | MF_mMFFMFM
2 | MF_MM_fMFM
3 | MF_MM_FM_m
4 | M__mM_FM_M
5 | M__MM__m_M
4)
1
0
245
Returns: "M"
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
198.97
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
class PeopleCircle
{
public:string order(int numMales, intnumFemales, int K)
{
int circle[60]={0},num=numMales+numFemales;
int pos=-1,count=0,temp;
while (count!=numFemales)
{
temp=0;
while(temp!=K)
{
pos=(pos+1)%num;
if (circle[pos]==0) temp++;
}
circle[pos]=1;
count++;
}
stringstream ans;
for(int i=0;i<num;i++)
if (circle[i]==0)
ans<<"M";
else ans<<"F";
return ans.str();
}
};
DIV 2
250
Problem Statement
Julius Caesar used a system ofcryptography, now known as Caesar Cipher, which shifted each letter 2 placesfurther through the alphabet (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.).At the end of the alphabet we wrap around, that is 'Y' shifts to 'A'.
We can, of course, try shifting by anynumber. Given an encoded text and a number of places to shift, decode it.
For example, "TOPCODER" shiftedby 2 places will be encoded as "VQREQFGT". In other words, if given(quotes for clarity) "VQREQFGT" and 2 as input, you will return"TOPCODER". See example 0 below.
Definition
Class:
CCipher
Method:
decode
Parameters:
string, int
Returns:
string
Method signature:
string decode(string cipherText, int shift)
(be sure your method is public)
Constraints
-
cipherText has between 0 to 50 charactersinclusive
-
each character of cipherText is anuppercase letter 'A'-'Z'
-
shift is between 0 and 25 inclusive
Examples
0)
"VQREQFGT"
2
Returns: "TOPCODER"
1)
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10
Returns:"QRSTUVWXYZABCDEFGHIJKLMNOP"
2)
"TOPCODER"
0
Returns: "TOPCODER"
3)
"ZWBGLZ"
25
Returns: "AXCHMA"
4)
"DBNPCBQ"
1
Returns: "CAMOBAP"
5)
"LIPPSASVPH"
4
Returns: "HELLOWORLD"
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
234.20 Code
#include<string>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;
class CCipher
{
public:string decode(string cipherText, intshift)
{
stringstream ans;
for(int i=0;i<cipherText.size();i++)
{
int temp=(cipherText[i]-'A')-shift;
if (temp<0) temp+=26;
char ch=temp+'A';
ans<<ch;
}
return ans.str();
}
};