Posted in GCJ

GCJ Post #4

Continuing with my said goal ( solving 5 problems per day) This is what I did for the 2 days…

((งง •̀•̀__•́•́))งง

I had planned to put 5 problems of the GCJ types and discuss post and lot picked one for the next 2 weeks until July 10th.But I suddenly felt that I was going too easy on myself , so I started doing 20 sums a day since yesterday until I am not able to do 20 sums per day…

So the list of problems that were chosen the last 2 days were:

  1. Count steps in Matrix
  2. Who dares to be a Millionaire
  3. Count Subarrays
  4. Malvika is peculiar about color of balloons
  5. Gregorian Calendar
  6. Chef and Subarrays
  7. Forgotten Language
  8. Chef and Strings
  9. Cooking Machine
  10. Brackets
  11. Chef and his sequence
  12. Mahasena
  13. Devu and Grapes
  14. Chef and Fruits
  15. Version Control System
  16. Cops and the thief Devu
  17. Good Joke!
  18. Farmer and his plot
  19. Chef and dolls
  20. Maximum and Minimum
  21. Chef and two strings
  22. Piece of cake
  23. Chef and the wildcard matching
  24. Sum of Palindromic Numbers
  25. Grade the steel
So the lot picked problem for today is 

Forgotten Language

The problem statement :

Forgotten languages (also known as extinct languages) are languages that are no longer in use. Such languages were, probably, widely used before and no one could have ever imagined that they will become extinct at some point. Unfortunately, that is what happened to them. On the happy side of things, a language may be dead, but some of its words may continue to be used in other languages.

Using something called as the Internet, you have acquired a dictionary of N words of a forgotten language. Meanwhile, you also know K phrases used in modern languages. For each of the words of the forgotten language, your task is to determine whether the word is still in use in any of these K modern phrases or not.


The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of a test case description contains two space separated positive integers N and K.

The second line of the description contains N strings denoting a dictionary of the forgotten language.

Each of the next K lines of the description starts with one positive integer L denoting the number of words in the corresponding phrase in modern languages. The integer is followed by L strings (not necessarily distinct) denoting the phrase.


For each test case, output a single line containing N tokens (space-separated): if the ith word of the dictionary exists in at least one phrase in modern languages, then you should output YES as the ith token, otherwise NO.


1 = T = 20
1 = N = 100
1 = K, L = 50
1 = length of any string in the input = 5



3 2
piygu ezyfo rzotm
1 piygu
6 tefwz tefwz piygu ezyfo tefwz piygu
4 1
kssdy tjzhy ljzym kegqz
4 kegqz kegqz kegqz vxvyj




First, you just need to store all the N words of the forgotten language and all the phrases. For each word, then, you can traverse over all phrases and check if its present in any one of them. You can also build a single array of all the words from all phrases and then check for each forgotten language word if its present in the array or not.

If you want it to more efficient you can store all phrase words in a set where insertion is O(log(size))O(log(size)) and checking if a string is present or not is also O(log(size))O(log(size)).

For implementation, you need to know basic programming concepts and syntax of a language of your choice. You should know about arrays, conditionals, loops, input/output to solve this problem.

So the code I came up with is this…well it works so I have no issue I guess…? 

for t in range(int(input())):
    N,K = (map(int, input().split()))
    forgotten = input().split()
    D = set()
    for i in range(K):
        text = list(input().split())
    for elem in forgotten :
        if elem in D:
            print(“YES”,end=” “)
            print(“NO”,end=” “)


{zzz}°°°( -_-)>c[_]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s