Problem:
We have a chat application.
Allowed operations are:
1> Adding a person to chat group, format +priyanka
2> Removing a person to chat group, format -sonali
3> Sending a message to chat group, format priyanka:Hi
Now, no traffic is sent to server for adding or removing person. But for every sent message, K bytes(length of message) for each person present in group are sent to server. Like if only Priyanka is there is a group, then Priyanka:Hello will send 5 bytes. If 2 persons are there in group, Priyanka:Hello will send 5x2=10 bytes.
Find total bytes sent to server.
Note: All input data is correct. A person cannot be added if it is already there in group. A person cannot be removed if he/she is not there is group. No two persons have same name.
Allowed operations are:
1> Adding a person to chat group, format +priyanka
2> Removing a person to chat group, format -sonali
3> Sending a message to chat group, format priyanka:Hi
Now, no traffic is sent to server for adding or removing person. But for every sent message, K bytes(length of message) for each person present in group are sent to server. Like if only Priyanka is there is a group, then Priyanka:Hello will send 5 bytes. If 2 persons are there in group, Priyanka:Hello will send 5x2=10 bytes.
Find total bytes sent to server.
Note: All input data is correct. A person cannot be added if it is already there in group. A person cannot be removed if he/she is not there is group. No two persons have same name.
Input Format:
Instructions in each line:
+Mohan
+shyam
-shyam
Mohan:Hello how r u
+Mohan
+shyam
-shyam
Mohan:Hello how r u
Output Format:
total bytes sent to serverConstraints:
NoneSample Input
+priya
+shikha
priya:hey shikha, how r u
shikha:fine, thanks
shikha:lets add sonali
+sonali
shikha:hi sonali
sonali:shikha, personal talks, remove priya
-priya
shikha:tell me now
+shikha
priya:hey shikha, how r u
shikha:fine, thanks
shikha:lets add sonali
+sonali
shikha:hi sonali
sonali:shikha, personal talks, remove priya
-priya
shikha:tell me now
Sample Output:
249Explanations:
92(when 2 people in chat) + 135(when 3 people in chat) + 22(when again 2 people). total=249
Solution:
$ans=0;
my $input;
while($input=<>)
{
@arr=split(//,$input);
if($arr[0] eq '+'){$c++;next;}
if($arr[0] eq '-'){$c--;next;}
$len=@arr;
$k=$len;
for($i=0;$i<$k;$i++)
{
$len--;
if($arr[$i] eq ':')
{
$len--;
$ans=$ans+($len*$c);
last;
}
}
}
print $ans;
Tips:
No use to maintain HASH for different names since we are just concerned about number of people in chat, not the names of person.
No comments:
Post a Comment