Algorithm
Problem Name: URI Online Judge | 2061
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2061
Closing Tabs
By Lucas Hermann Negri, UTFPR Brazil
Timelimit: 1
Péricles has an unique interest in history. With his up-to-date internet browser chromed fox, he wandered in the most obscure sites about ancient Greek mythology.
By some type of cosmic irony, Péricles' browser was infected by a malware with a peculiar characteristic: every time Péricles closed a tab in his browser, another two opened! However, when Péricles clicked one of the ads (all tabs were infested with ads), the tab crashed, and no other tabs were opened.
Your taks is to compute the final number of tabs of Péricles's browser, knowing the initial number of tabs and the actions taken by Péricles. There are two possible actions: fechou (when Péricles closed a tab) and clicou (when Péricles clicked on an ad).
Input
The input is initiated by a line containing two integer numbers, N and M (0 < N, M < 500), representing the initial number of tabs and the number of actions performed by Péricles. Each subsequent line contains an action (fechou or clicou). Naturally, the current number of tabs is always greater or equal to zero.
Output
The output must consist of a line containing the final number of tabs.
Input Sample | Output Sample |
3 5 |
2 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int main(void)
{
int i, j, a, b, n, x, sum;
char str[7];
scanf("%i %i", &a, &n);
sum = a;
for (i = 0; i < n; ++i)
{
scanf("%s", str);
x=strcmp(str, "fechou");
if (x==0)
sum++;
else
sum--;
}
printf("%i\n", sum);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include<stdio.h>
#include<string.h>
int main()
{
int i,initial,test,num=0,attempt=0,cancel=0,final;
char ab[10];
scanf("%d %d",&initial,&test);
for(i = 0; i < test; i++)
{
scanf("%s",&ab);
if(ab[0]=='f')
{
num = num+2;
attempt++;
}
else if(ab[0]=='c')
{
cancel++;
}
}
final = (initial + num - attempt - cancel );
printf("%d\n",final);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
let [x, y] = lines.shift().split(" ");
let action;
for(let i = 0; i < y; i++){
action = lines.shift().trim();
if(action == "fechou"){
x++;
}
else{
x--;
}
}
console.log(x);
Copy The Code &
Try With Live Editor
Input
Output