Algorithm
Problem Name: beecrowd | 3161
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/3161
The Forgotten Fruits
By Elder Sobrinho, UFTM Brazil
Timelimit: 5
Sheldon Cooper is an eccentric character in the Hollywood universe. He recently had an accident and ended up forgetting which fruits he likes to eat. However, the time has come to prepare breakfast and Sheldon does not want to go through the experience of tasting fruit and discovering that he does not like it. Then, bothered by this situation, Sheldon convinced his friend Leonard Hofstadter to help him. Leonard remembers the moment he met Sheldon and due to his friend's eccentricities, he kept on his computer a list with the name of the fruits that Sheldon likes to eat. Leonard, very excited by his wit, opens the file and notes that something is wrong: the contents of the file have been scrambled by a computer virus. Determined to resolve this issue, Leonard experimented and concluded that it is possible to read the contents of the file and find out whether Sheldon likes a particular fruit or not. From experiments, Leonard noted that the virus made some (s) of the following changes: 1) Added new characters to the left and/or right to the name of the fruit that was on the list; 2) Changed some letters, in this case, some became uppercase and others lowercase; 3) The name of the fruit on the list has been inverted ("Bergamota" => "Atomagreb"). As Leonard studied programming, he will create a program that is named after a fruit and returns whether Sheldon likes that fruit or not.
Input
The first line contains two integers: 1) N that represents the number of fruit names that will be checked/searched, limited by [1,100]; 2) M which represents the number of lines in the list of fruit names, limited by [15,500]; In addition, each M line of the name list and each N line with the fruit name follow the limit: [4,100].
Output
For each fruit searched, show the output: "Sheldon come a fruta X" or "Sheldon detesta a fruta X" (where X is the name of the fruit that was verified in the list, in lowercase).
Input Sample | Output Sample |
6 15 |
Sheldon come a fruta jenipapo |
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const [fruitsQuantity, textQuantity, ...lines] = readFileSync("/dev/stdin", "utf8")
.split(/\s+/)
.map((str) => str.toLowerCase())
const reverseString = (str = "") => [...str].reverse().join("")
function main() {
const fruitSize = Number.parseInt(fruitsQuantity, 10)
const textSize = Number.parseInt(textQuantity, 10)
const fruits = lines.slice(0, fruitSize)
const contents = lines.slice(fruitSize, fruitSize + textSize)
const result = fruits.map((fruit) => {
const has = contents.some((line) => {
return line.includes(fruit) || line.includes(reverseString(fruit))
})
return `Sheldon ${has ? "come" : "detesta"} a fruta ${fruit}`
})
console.log(result.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Python Programming
Code -
Python Programming
n,m=map(int,input().split())
vet_n,vet_m=[],[]
for i in range(n):vet_n.append(input().lower())
for i in range(m):vet_m.append(input().lower())
for i in vet_n:
aux,aux_inv=False,i[::-1]
for j in vet_m:
if i in j or aux_inv in j:
aux=True
break
print("Sheldon come a fruta", i) if aux else print("Sheldon detesta a fruta", i)
Copy The Code &
Try With Live Editor
Input
Output