Algorithm
Problem Name: beecrowd | 2724
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2724
Help Patatatitu
By Felipe C. Ochial, URI Brazil
Timelimit: 1
Juvenal behaved exemplary this year, since he likes chemistry deeply and really want to earn an Alchemy kit. However, Juvenal asked to include some dangerous elements in his kit. As Santa could not deny the request ( how to say no to the world most well behaved children?) asked to poor elf Patatatitu to ensure that the present was safe.
Patatatitu knows a lot about chemistry, and knows every dangerous compound that can be made with the elements available on Juvenal’s kit. Thus, he decided to send a cd together with the gift, containing a program which asserts the safety of Juvenal’s experiments. Everyone agrees that the world’s most well behaved children would never do an experiment without first checking it’s safety as per Santa instructions. However Patatatitu knows nothing about programming and is after someone to help him. Can you help?
To elucidate, Patatatitu explains that a dangerous compound are formed from a mix of elements in theirs chemical formula respecting it’s order and proportions. In this kit it’s possible to add one element each time, in various quantities. Thus, to form chlorine trifluoride (ClF3), an extremely dangerous compound, you must add an atom of chlorine (Cl) and three of fluorine (F3), regardless of what was added before or after. ClF4 is not a dangerous compound since it’s a different proportion from ClF3. Similarly, if Mg2F is a dangerous compound, Mg2Fe is safe, since fluorine (F) is different from iron (Fe).
Input
The input consist of an integer N (0 < N < 10) which indicates the number of test cases. Each test case have an integer T (0 < T < 51) which indicates the number of dangerous compounds possible, if th elements are included in the order and proportions shown. Follow T lines, each containing a string up to 50 characters representing a formula that generates a dangerous compound if the elements are added in that particular order and proportion. After, is given an integer U (0 < T < 51) that indicates the number of experiments Juvenal will do. Follow U lines each containing an string up to 50 characters representing the elements that Juvenal will use in the order and proportions as they are added.
Output
The output consist of U per test case, which must inform if Juvenal must abort it’s experiment or proceed with the U-th experiment of the test case. If Juvenal must abort print “Abortar”, else if it’s safe print “Prossiga”.Test cases must be separated by a blank line .
Input Sample | Output Sample |
3 3 KH2O C3H5N3O9 ClF3 5 WOsFNeSeBrSnAsNOH4C12CuKZrBr C8H10N4O2C2H7NO3SC6H5NO2 C3H5N3O9ClF3KH20 C3H5N3O9 4P12Si7CNF12BLiClF312ON12H 2 H20NaCl C6H12F2 4 H20Na C6H12F H20NaCl C6H12F2 3 KBrAsC Mg2F CsH 6 KBrAsCl Mg2Fe CsHe Mg2F Cl2NaOPMg2F KBrAsC |
Prossiga Prossiga Abortar Abortar Prossiga Prossiga Prossiga Abortar Abortar Prossiga Prossiga Prossiga Abortar Abortar Abortar |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<math.h>
#include<string>
#include<list>
using namespace std;
#define ll long long
#define input scanf
#define output printf
#define Loop while
#define echo cout
#define ret return
#define MAX 999999999999999999
#define MIN 0
#define PI 3.1415
int main(int argc, char** argv) {
//freopen("c.txt","w",stdout);
int n,t,u;
scanf("%d",&n);
bool sp=false;
while(n--)
{
if(sp)cout << endl;
sp=true;
scanf("%d",&t);
vector < string> che(t);
cin.ignore();
for(int i = 0;i < t; i++)
{
cin >> che[i];
}
scanf("%d",&u);
cin.ignore();
string str;
while(u--)
{
cin >> str;
bool harm=false;
size_t found;
for(int i = 0; i < t; i++)
{
found=str.find(che[i]);
if(found!=string::npos)
{
int index=found;
index+=che[i].length();
if(str.length()>=index&&((str[index]>=97
&&str[index]<=122)||(isdigit(str[index]))))
{
continue;
}
cout << "Abortar\n";
//cout << found << endl;
harm=true;
break;
}
}
if(!harm>cout << "Prossiga\n";
}
}
ret 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
const upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function isDangerous(C) {
for (let element of dangerous) {
if (C.includes(element)) {
let aux = C.split(element);
for (let m = 1; m < aux.length; m++) {
if (upperCases.includes(aux[m].charAt(0))) {
return true;
}
}
}
}
return false;
}
const testCases = parseInt(prompt("testCases"));
for (let i = 0; i < testCases; i++) {
var dangerousAmmount = parseInt(prompt("dangerousSize"));
var dangerous = [];
for (let n = 0; n < dangerousAmmount; n++) {
dangerous.push(prompt());
}
var compoundsAmmount = parseInt(prompt("compoundsSize"));
for (let n = 0; n < compoundsAmmount; n++) {
var compound = prompt();
if (isDangerous(compound)) {
console.log("Abortar");
} else {
console.log("Prossiga");
}
}
if (i != testCases - 1) {
console.log("");
}
}
Copy The Code &
Try With Live Editor
Input
Output