Many internet protocols these days include the option of associating a
media type with the content being sent.
The type is usually inferred from the file extension.
You are to write a program that facilitates the lookup of media types for
a number of files.
You will be given a table of media type associations that associate a certain
file extension with a certain media type.
You will then be given a number of file names, and tasked to determine the correct
media type for each file.
A file extension is defined as the part of the file name after the final period.
If a file name has no periods, then it has no extension and the media type cannot
be determined.
If the file extension is not present in the table, then the media type cannot be
determined.
In such cases you will print "unknown" as the media type.
If the file extension does appear in the table (case matters), then print the associated
media type.

-----Input-----
Input begins with 2 integers N and Q on a line.
N is the number of media type associations, and Q is the number of file names.
Following this are N lines, each containing a file extension and a media type, separated by a space.
Finally, Q lines, each containing the name of a file.
N and Q will be no greater than 100 each.
File extensions will consist only of alphanumeric characters, will have length at most 10, and will be distinct.
Media types will have length at most 50, and will contain only alphanumeric characters and punctuation.
File names will consist only of alphanumeric characters and periods and have length at most 50.

-----Output-----
For each of the Q file names, print on a line the media type of the file.
If there is no matching entry, print "unknown" (quotes for clarity).

-----Sample Input-----
5 6
html text/html
htm text/html
png image/png
svg image/svg+xml
txt text/plain
index.html
this.file.has.lots.of.dots.txt
nodotsatall
virus.exe
dont.let.the.png.fool.you
case.matters.TXT

-----Sample Output-----
text/html
text/plain
unknown
unknown
unknown
unknown

Source: codechef | Difficulty: EASY
Tags: String algorithms

Data structures

Implementation


Solutions:
(n, q) = map(int, input().split())
file_types = {}
for _ in range(n):
	(ext, mime) = input().split()
	file_types[ext] = mime
for _ in range(q):
	filename = input().strip()
	found = False
	for (ext, mime) in file_types.items():
		if filename.endswith('.' + ext):
			print(mime)
			found = True
			break
	if not found:
		print('unknown')


(n, k) = [int(d) for d in input().split()]
datadict = {}
for i in range(n):
	data = input().split(' ')
	datadict[data[0]] = data[1]
for i in range(k):
	ans = 'unknown'
	data = input()
	if data.count('.') > 0:
		ddata = data.strip().split('.')
		if ddata[-1] in datadict:
			ans = datadict[ddata[-1]]
	print(ans)


(n, q) = [int(i) for i in input().split()]
files = {}
for i in range(n):
	(fexT, mType) = input().split()
	files[fexT] = mType
for i in range(q):
	ans = 'unknown'
	file = input()
	if file.count('.') > 0:
		ext = file.strip().split('.')
		if ext[-1] in files:
			ans = files[ext[-1]]
	print(ans)


(n, q) = list(map(int, input().split()))
x = {}
for j in range(n):
	(a, b) = list(map(str, input().split()))
	x[a] = b
for i in range(q):
	h = input().strip()
	if '.' in h:
		c = h.split('.')[-1]
		if c in x:
			print(x[c])
		else:
			print('unknown')
	else:
		print('unknown')


def __starting_point():
	(N, Q) = list(map(int, input().strip().split()))
	d = {}
	for _ in range(N):
		(a, b) = input().strip().split()
		d[a] = b
	for _ in range(Q):
		text = input().strip()
		idx = text.rfind('.')
		if idx >= 0 and text[idx + 1:] in d:
			print(d[text[idx + 1:]])
		else:
			print('unknown')
__starting_point()


(n, q) = map(int, input().split())
d = dict()
for _ in range(n):
	(ext, med) = map(str, input().split())
	d[ext] = med
for _ in range(q):
	s = input().strip()
	if '.' in s:
		ext = s.split('.')[-1]
		if ext in d:
			print(d[ext])
		else:
			print('unknown')
	else:
		print('unknown')


try:
	(n, q) = map(int, input().split())
	d = {}
	for _ in range(n):
		(i, j) = input().split()
		d[i] = j
	for _ in range(q):
		case = 0
		s = input().strip()
		for (ext, name) in d.items():
			if s.endswith('.' + ext):
				print(name)
				case = 1
				break
		if case == 0:
			print('unknown')
except:
	print('error')


(n, q) = map(int, input().split())
file_type = {}
for i in range(n):
	(key, value) = map(str, input().split())
	file_type[key] = value
for i in range(q):
	text = str(input().strip())
	file = ''
	for j in range(len(text) - 1, -1, -1):
		if text[j] == '.':
			file = text[j + 1:]
			break
	if file == '':
		print('unknown')
		continue
	elif file in file_type.keys():
		print(file_type[file])
	else:
		print('unknown')


(n, q) = list(map(int, input().split(' ')))
f = []
m = []
for _ in range(n):
	(a, b) = input().split()
	f.append(a)
	m.append(b)
for _ in range(q):
	s = input().strip()
	if '.' not in s:
		print('unknown')
	else:
		ext = s.split('.')[-1]
		if ext in f:
			print(m[f.index(ext)])
		else:
			print('unknown')


(p, q) = map(int, input().split())
l = {}
for i in range(p):
	(i, j) = input().split()
	l[i] = j
for i in range(q):
	s = input().strip()
	c = 0
	for (k, v) in l.items():
		if s.endswith('.' + k):
			c += 1
			print(v)
			break
	if c == 0:
		print('unknown')
l.clear()


(n, q) = map(int, input().split())
x = {}
for _ in range(n):
	(a, b) = map(str, input().split())
	x[a] = b
for _ in range(q):
	h = input().strip()
	if '.' in h:
		c = h.split('.')[-1]
		if c in x:
			print(x[c])
		else:
			print('unknown')
	else:
		print('unknown')


(n, q) = map(int, input().split())
x = dict((input().strip().split() for i in range(n)))
for j in range(q):
	a = list(input().strip().split('.'))
	if len(a) == 1:
		print('unknown')
	elif a[-1] in x:
		print(x[a[-1]])
	else:
		print('unknown')


(n, m) = map(int, input().split())
a = {}
for i in range(n):
	(x, y) = input().split()
	a[x] = y
for i in range(m):
	c = input().strip()
	if '.' not in c:
		print('unknown')
	else:
		h = c.split('.')[-1]
		if h in a:
			print(a[h])
		else:
			print('unknown')


(p, q) = map(int, input().split())
l = {}
for i in range(p):
	(i, j) = input().split()
	l[i] = j
for i in range(q):
	s = input().strip()
	if s.count('.') == 0:
		print('unknown')
		continue
	a = s[-1::-1].index('.')
	b = s[-a:]
	c = 0
	for (k, v) in l.items():
		if k == b:
			c += 1
			print(v)
			break
	if c == 0:
		print('unknown')
l.clear()


(n, q) = map(int, input().split())
d = {}
for i in range(n):
	(a, b) = map(str, input().split())
	d[a] = b
for j in range(q):
	c = input().strip()
	if '.' in c:
		f = c.split('.')
		if f[-1] in d:
			print(d[f[-1]])
		else:
			print('unknown')
	else:
		print('unknown')


(a, b) = map(int, input().split())
d = {}
for i in range(a + b):
	if i < a:
		s = input().split()
		d[s[0]] = s[1]
	else:
		k = ''
		s = input().strip()
		for j in range(len(s) - 1, -1, -1):
			if s[j] == '.':
				k = s[j + 1:]
				break
		if k in d:
			print(d[k])
		else:
			print('unknown')


d = {}
(n, q) = list(map(int, input().split()))
for _ in range(n):
	(a, b) = input().strip().split()
	d[a] = b
for _ in range(q):
	f = input().strip()
	if '.' in f:
		print(d.get(f.split('.')[-1], 'unknown'))
	else:
		print('unknown')


str = input()
(n, q) = map(int, str.split())
typ_lst = {}

def chk(file):
	if '.' in file:
		lst = list(file.split('.'))
		if lst[-1] in typ_lst:
			return typ_lst[lst[-1]]
	return 'unknown'
for i in range(n):
	str = input()
	(exe, typ) = str.split()
	typ_lst[exe] = typ
for i in range(q):
	file = input().strip()
	print(chk(file))


(x, y) = map(int, input().split())
a = {}
for i in range(x):
	(p, q) = input().split()
	a[p] = q
for i in range(y):
	f = input().strip()
	if '.' in f:
		print(a.get(f.split('.')[-1], 'unknown'))
	else:
		print('unknown')


(n, q) = (int(i) for i in input().split())
file_types = {}
for _ in range(n):
	(ext, mime) = input().split()
	file_types[ext] = mime
for _ in range(q):
	filename = input().strip()
	if '.' in filename:
		ext = filename.split('.')[-1]
		try:
			print(file_types[ext])
		except KeyError:
			print('unknown')
	else:
		print('unknown')


(n, k) = map(int, input().split())
f = {}
for i in range(n):
	(extension, typ) = input().split()
	f[extension] = typ
for i in range(k):
	s = input()
	unknown = True
	if s.count('.') > 0:
		l = s.strip().split('.')
		if l[-1] in f:
			unknown = False
			print(f[l[-1]])
	if unknown:
		print('unknown')


(n, q) = map(int, input().split())
d = {}
for i in range(n):
	s = input()
	(filext, medtyp) = s.split(' ')
	d[filext] = medtyp
for i in range(q):
	s = input()
	a = 'unknown'
	if s.count('.') > 0:
		l = s.strip().split('.')
		if l[-1] in d:
			a = d[l[-1]]
	print(a)


(n, q) = list(map(int, input().split()))
extensions = {}
for _ in range(n):
	(ext_name, ext_type) = input().split()
	extensions[ext_name] = ext_type
for _ in range(q):
	filename = input().strip()
	if '.' in filename:
		ext = filename.split('.')[-1]
		if extensions.get(ext) != None:
			print(extensions[ext])
		else:
			print('unknown')
	else:
		print('unknown')


(n, q) = map(int, input().split())
d = dict()
for _ in range(n):
	(ext, med) = map(str, input().split())
	d[ext] = med
for _ in range(q):
	s = input().strip()
	s = str(s[::-1])
	ext = ''
	for i in range(len(s)):
		if s[i] != '.':
			ext += s[i]
		else:
			break
	ext = str(ext[::-1])
	if ext in d and '.' in s:
		print(d[ext])
	else:
		print('unknown')


(n, q) = map(int, input().split())
l = {}
for i in range(n):
	(a, b) = input().split()
	l[a] = b
for i in range(q):
	s = input().strip()
	if '.' in s:
		s = s.split('.')
		x = s[-1]
		if x in l:
			print(l[x])
		else:
			print('unknown')
	else:
		print('unknown')


(n, q) = list(map(int, input().split(' ')))
files = []
media = []
for _ in range(n):
	(a, b) = input().split(' ')
	files.append(a)
	media.append(b)
for _ in range(q):
	s = input().strip()
	if len(s.split('.')) <= 1:
		print('unknown')
	else:
		ext = s.split('.')[-1]
		if ext in files:
			print(media[files.index(ext)])
		else:
			print('unknown')


(n, q) = map(int, input().split())
d = {}
for _ in range(n):
	(ex, mt) = input().split(' ')
	d[ex] = mt
for i in range(q):
	name = input()
	l = name.strip().split('.')
	if len(l) <= 1 or l[-1] not in d.keys():
		print('unknown')
	else:
		print(d[l[-1]])


(u, y) = map(int, input().split())
f = {}
for i in range(u):
	(extension, typ) = input().split()
	f[extension] = typ
for i in range(y):
	s = input()
	unknown = True
	if s.count('.') > 0:
		l = s.strip().split('.')
		if l[-1] in f:
			unknown = False
			print(f[l[-1]])
	if unknown:
		print('unknown')


(n, q) = [int(x) for x in input().split(' ')]
file_assc = {}
for i in range(n):
	(ext, Mtype) = input().split(' ')
	file_assc[ext] = Mtype
for i in range(q):
	f = input().strip()
	if f.find('.') != -1:
		fname = f.split('.')[-1]
		if fname in file_assc:
			print(file_assc[fname])
		else:
			print('unknown')
	else:
		print('unknown')


(n, i, j, d) = (list(map(int, input().split())), 1, 1, {})
while i <= n[0]:
	extension = list(input().split())
	d[extension[0]] = extension[1]
	i += 1
while j <= n[1]:
	try:
		filenames = input()
		if filenames.find('.') == -1:
			print('unknown')
		else:
			x = filenames.strip().split('.')
			if x[-1] not in d.keys():
				print('unknown')
			else:
				print(d[x[-1]])
		x = []
		j += 1
	except EOFError:
		break


(types, files) = map(int, input().split())
types_dict = {}
for i in range(types):
	arr = input().split()
	types_dict[arr[0]] = arr[1]
for i in range(files):
	k = ''
	s = input().strip()
	for j in range(len(s) - 1, -1, -1):
		if s[j] == '.':
			k = s[j + 1:]
			break
	if k in types_dict:
		print(types_dict[k])
	else:
		print('unknown')


(n, m) = [int(x) for x in input().split()]
d = {}
for i in range(n):
	(s, s1) = [str(x) for x in input().split()]
	d[s] = s1
for j in range(m):
	a = input().strip().split('.')
	if len(a) > 1 and a[-1] in d:
		print(d[a[-1]])
	else:
		print('unknown')


(N, Q) = map(int, input().split())
dic = {}
for i in range(N):
	(a, b) = input().split()
	dic[a] = b
for kk in range(Q):
	a = input().strip()
	ext = ''
	if '.' in a:
		ext = a.split('.')[-1]
		if ext in dic.keys():
			print(dic[ext])
		else:
			print('unknown')
	else:
		print('unknown')


(q, w) = map(int, input().split())
f = {}
for e in range(q):
	(extension, typ) = input().split()
	f[extension] = typ
for e in range(w):
	s = input()
	unknown = True
	if s.count('.') > 0:
		l = s.strip().split('.')
		if l[-1] in f:
			unknown = False
			print(f[l[-1]])
	if unknown:
		print('unknown')


(a, b) = map(int, input().split())
sample_dict = {}
for i in range(a):
	x = input().strip().split()
	sample_dict[x[0]] = x[1]
for i in range(b):
	x = input().strip()
	if len(x.split('.')) == 1:
		print('unknown')
	elif x.split('.')[-1] in sample_dict.keys():
		print(sample_dict[x.split('.')[-1]])
	else:
		print('unknown')


dict = {}
(a, b) = map(int, input().split())
for i in range(a):
	(c, d) = input().split()
	dict[c] = d
for i in range(b):
	a = input().strip().split('.')
	if len(a) <= 1 or a[-1] not in dict.keys():
		print('unknown')
	else:
		print(dict[a[-1]])


(n, q) = map(int, input().split())
t = {}
for _ in range(n):
	(a, b) = input().split()
	t[a] = b
for _ in range(q):
	s = input().strip()
	if '.' not in s:
		print('unknown')
	else:
		ext = s.split('.')[-1]
		if ext in t:
			print(t[ext])
		else:
			print('unknown')


(n, q) = map(int, input().split())
a = {}
b = []
for i in range(n):
	(g, h) = input().split(' ')
	a.update({g: h})
for i in range(q):
	k = ''
	s = input().strip()
	for j in range(len(s) - 1, -1, -1):
		if s[j] == '.':
			k = s[j + 1:]
			break
	if k in a:
		print(a[k])
	else:
		print('unknown')


(types, files) = map(int, input().split())
types_dict = {}
for i in range(types):
	arr = list(input().split())
	types_dict[arr[0]] = arr[1]
for i in range(files):
	string = input()
	ans = 'unknown'
	if string.count('.') > 0:
		temp_arr = string.strip().split('.')
		if temp_arr[-1] in types_dict:
			ans = types_dict[temp_arr[-1]]
	print(ans)


(n, q) = list(map(int, input().split()))
d = {}
l = []
for m in range(n):
	(a, b) = input().split()
	d[a] = b
for m in range(q):
	flag = 0
	s = input().strip()
	for (ext, name) in d.items():
		if s.endswith('.' + ext):
			print(name)
			flag = 1
	if flag == 0:
		print('unknown')


(N, Q) = map(int, input().split())
table = {}
for i in range(N):
	(fileExt, fileType) = input().split()
	table[fileExt] = fileType
for q in range(Q):
	fileName = input().strip()
	index = -1
	for i in range(len(fileName)):
		if fileName[i] == '.':
			index = i
	if index == -1:
		print('unknown')
		continue
	extension = fileName[index + 1:]
	if extension in table:
		print(table[extension])
	else:
		print('unknown')


(n, q) = map(int, input().split())
data = {}
for _ in range(n):
	(d1, d2) = input().strip().split()
	data[d1] = d2
for _ in range(q):
	s = input().strip()
	l = s.split('.')
	if len(l) <= 1:
		print('unknown')
	elif l[-1] in data.keys():
		print(data[l[-1]])
	else:
		print('unknown')


(N, Q) = map(int, input().split())
types = {}
for _ in range(N):
	(K, V) = input().split()
	types[K] = V
for _ in range(Q):
	fileName = input().strip()
	I = fileName.rfind('.')
	if I != -1:
		extension = fileName[I + 1:]
		if extension in types.keys():
			print(types[extension])
		else:
			print('unknown')
	else:
		print('unknown')


(n, q) = input().split()
res = {}
for i in range(int(n)):
	data = input().split()
	res[data[0]] = data[1]
for i in range(int(q)):
	val = input().strip()
	for (key, value) in res.items():
		if val.endswith('.' + key):
			print(res[key])
			break
	else:
		print('unknown')
res.clear()


(n, q) = map(int, input().split())
d = {}
l = []
for _ in range(n):
	(i, j) = input().split()
	d[i] = j
for _ in range(q):
	flag = 0
	s = input().strip()
	for (ext, name) in d.items():
		if s.endswith('.' + ext):
			print(name)
			flag = 1
			break
	if flag == 0:
		print('unknown')