problems / 204

Document Revision Manager

easy design hashing simulation 1000 ms · 256 MB

Document Revision Manager

You are implementing a simple document revision system for a collaborative editor. The system must support creating and retrieving different versions of documents, and analyzing what changed between versions.

Implement a version-control manager that supports three operations:

  1. EDIT id content: Create or update the document at version id with the given content. If version id exists, overwrite it; otherwise, create it.

  2. GET id: Return the content stored at version id. If the version doesn't exist, output "VERSION NOT FOUND".

  3. DIFF id1 id2: Compare the content of version id1 and version id2. Output the number of lines that are different.

Input

The first line contains n (1 ≤ n ≤ 1000), the number of operations.

Each of the next n lines contains one operation:

  • EDIT <id> <content>: where id is a positive integer (1 ≤ id ≤ 10^6) and content is a single line of text: 1 to 1000 characters, each an English letter (a-z, A-Z), a digit or a space, with no leading or trailing spaces and no two consecutive spaces. The content is everything after the space that follows id, and it may contain several words.
  • GET <id>: where id is a positive integer (1 ≤ id ≤ 10^6).
  • DIFF <id1> <id2>: where id1 and id2 are positive integers (1 ≤ id1, id2 ≤ 10^6).

Output

For each GET operation, output the content of the version or "VERSION NOT FOUND".

For each DIFF operation, output a single integer: the number of differing lines between the two versions.

Notes

  • Each version stores a single line of text.
  • When comparing two versions with DIFF, if the content is identical, the answer is 0. If they differ, the answer is 1 (since each version is one line, it either changed or stayed the same).
  • If either version in a DIFF operation doesn't exist, treat it as an empty string.

Example 1

Input

8
EDIT 1 hello
EDIT 2 world
GET 1
GET 2
DIFF 1 2
EDIT 1 hello
GET 1
DIFF 1 2

Output

hello
world
1
hello
1

Example 2

Input

7
EDIT 5 apple
EDIT 5 apple
GET 5
DIFF 5 5
GET 999
DIFF 10 20
DIFF 5 10

Output

apple
0
VERSION NOT FOUND
0
1

Hints · reveal one at a time if you're stuck

Hint 1

Each version holds exactly one line, so what do you need to remember per version id? Which structure gives fast lookup by id?

Hint 2

Parse each command by splitting off only the first word or two, because the content may contain spaces. For DIFF, a missing version behaves like an empty string, so what does DIFF give for two missing ids?

report an issue Prefer your own editor? sooks prep 204 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.