Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kattis prob pizzahawaii in python3 + notes
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
A sets/dictionaries problem. | ||
|
||
Build up a dictionary `d` of possible translations for each foreign word. | ||
|
||
Whenever a new pizza is read in, the possible translations for each `w` in the foreign language should be the set intersection of the previous translations (`d[w]`) and the english ingredients in this pizza. | ||
|
||
After this is done for all pizzas, you need to run through each `w` in `d` and remove all possible candidate translations that appear in other pizzas without `w`. ie. For every input pizza, if `w` is not in the foreign ingredients, then you should remove all the english ingredients for that pizza from `d[w]`. | ||
|
||
This runs in 0.02s in python3 (the fastest python3 submission on kattis!). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def main(): | ||
t = int(input()) | ||
for _ in range(t): | ||
n = int(input()) | ||
d = {} | ||
p = [] | ||
for _ in range(n): | ||
input() # pizza name | ||
_, *u = input().split() | ||
_, *v = input().split() | ||
u = set(u) | ||
v = set(v) | ||
for w in u: | ||
if w in d: | ||
d[w] &= v | ||
else: | ||
d[w] = set(v) | ||
p.append((u, v)) | ||
for u, v in p: | ||
for w in d: | ||
if w not in u: | ||
d[w] -= v | ||
ans = [] | ||
for k in d: | ||
for o in d[k]: | ||
ans.append((k, o)) | ||
ans.sort() | ||
for a in ans: | ||
print('({}, {})'.format(*a)) | ||
print() | ||
|
||
if __name__ == '__main__': | ||
main() |