INI Lookup
A legacy service keeps its settings in an INI file: a plain text file split into named sections,
each holding key = value settings. Your job is to read the file the way the service does and
answer a list of lookups.
File format
The file consists of n lines. Every line contains only printable ASCII characters (codes 32 to
126) and tabs. Below, whitespace means spaces and tabs, and to trim a string means to remove
all whitespace from its beginning and its end.
A section name is a string of 1 to 40 characters made of letters (A–Z, a–z), digits,
underscores _, hyphens - and spaces, which does not start or end with a space. A key name
is the same, except that it may also contain dots .. Neither may contain tabs. For example
Data Base and db-1 are section names, and server.port and max size are key names.
Each line has exactly one of the following forms:
- A blank line: empty, or whitespace only.
- A comment line: optional whitespace, then
;or#, then any text up to the end of the line. It is ignored. - A section header: optional whitespace,
[, optional whitespace, a section name, optional whitespace,], optional whitespace. All lines after it, up to the next section header, belong to that section. - A setting line: optional whitespace, a key name, optional whitespace,
=, then the raw value: any text up to the end of the line (possibly empty). The value of the setting is the raw value, trimmed. The value may contain any characters, including=,;,#,[and]; they are part of the value and never start a comment.
Setting lines before the first section header belong to the global section, whose name is the empty string.
Section names and key names are case-insensitive: [Server] and [SERVER] denote the same
section, and Port and port the same key. Spaces inside names are significant: max size and
maxsize are different keys. A section may have several headers in the file; its settings are
gathered from all of them. If the same key is set several times in the same section, the
last setting in the file wins.
Queries
Each query has the form S.K: a possibly empty string S (the section), a dot, and a key name
K. The query string is split at its first dot: S is everything before it and contains no
dots, while K may contain further dots. When S is empty, the query asks about the global
section. S is either empty or a section name; it, too, is matched case-insensitively and need
not appear in the file.
Input
The first line contains two integers n and q (1 ≤ n ≤ 10^5, 1 ≤ q ≤ 10^5), separated by a
single space.
The next n lines are the lines of the file, each well formed as described above and at most 200
characters long.
The next q lines contain one query each, with no leading or trailing whitespace.
Every line of the input is terminated by a line feed, except that the last line may or may not have one. There are no carriage returns. The whole input is at most 10^7 characters long.
Output
For each query, in order, print one line. If the section S has a setting for the key K, print
its value enclosed in double quotes "; the value is printed exactly as it appears in the file,
with its original letter case. Otherwise print NOT FOUND.
Notes
In the first example, name is a global setting, and Server.PORT finds port in section
server regardless of case. The key host is set twice in server, and the later value
10.0.0.2 wins. The key name is set only in the global section, not in section server, and
section cache does not exist.
In the second example (some of its lines contain tabs), the query db.conn.timeout asks for the
key conn.timeout in section db, which is set twice: the later setting, under the second header
of db, wins. The value of url is a=b;c # not a comment, because ; and # inside a value
are kept. The key empty exists with an empty value, which is printed as "". In section
Data Base, max size is overridden by the later MAX SIZE. The last query asks for the key
base.max size in section data, which does not exist.