Intellectual Property

Time limit1sMemory limit128 MB

Summary
Given two code bases as raw strings, find the k longest maximal substrings of the JCN base that also occur in the TDP base, with exact positions and lengths.
Level

Hard8 of 10

Topics
String matching, Sorting, Array, String
Solved
No attempts yet

Problem

TDP Inc. has decided to sue JCN Inc. for copyright infringement. To build its case, TDP wants to find the infringing segments inside JCN's code base so that it can present them to selected members of the press. Because TDP has let go of all of its technical staff, it plans to hire an outside consultant, paid on a contingency basis only if the lawsuit succeeds. To prove that you are qualified for the job, solve the following problem for a number of test cases.

Input

Each test case begins with a positive integer kk, the number of infringing segments to find.

After that line come two code bases. The first code base starts with the line BEGIN TDP CODEBASE, contains some number of lines, and ends with the line END TDP CODEBASE. The second code base starts with BEGIN JCN CODEBASE and ends with END JCN CODEBASE. The line END TDP CODEBASE never appears inside the first code base, and the line END JCN CODEBASE never appears inside the second.

A line containing a single 0 follows the last test case.

Output

For every test case, print:

  1. a line CASE n, where nn is the test case number (starting from 1);
  2. up to kk infringing segments.

Each segment must be printed exactly as it appears in the JCN code base, including any spaces and newline characters. Immediately before each segment, print a line

INFRINGING SEGMENT m LENGTH l POSITION p

where mm is the index of the segment within the current test case (starting from 1), ll is the length of the segment in characters, and pp is the position of the segment measured in characters from the start of the JCN code base (the first character is at position 0). Print one empty line between consecutive test cases.

A code base is simply a string of characters. An infringing segment is a non-empty, contiguous run of characters in the JCN code base that is textually identical to some contiguous run of characters in the TDP code base and that is not contained in any longer infringing segment. Every character counts, including spaces and the newline that terminates each line.

Order the segments by decreasing length. Segments of equal length are ordered by the position at which they occur in the JCN code base (earliest first). If there are kk or fewer segments, print all of them in this order; if there are more than kk, print only the first kk.

You may assume that no code base contains more than 50,000 characters.

Examples1

  1. Example 1

    Input
    6
    BEGIN TDP CODEBASE
    the quick brown fox
    jumps over the lazy dog.
    so there!
    END TDP CODEBASE
    BEGIN JCN CODEBASE
    now is the time for all
    good men to come to the aid
    of the party.
    so there!
    END JCN CODEBASE
    100
    BEGIN TDP CODEBASE
    xyzzy
    END TDP CODEBASE
    BEGIN JCN CODEBASE
    xyzzabczzyy
    END JCN CODEBASE
    0
    
    Expected output
    CASE 1
    INFRINGING SEGMENT 1 LENGTH 12 POSITION 64
    .
    so there!
    
    INFRINGING SEGMENT 2 LENGTH 5 POSITION 6
     the 
    INFRINGING SEGMENT 3 LENGTH 5 POSITION 42
    o the
    INFRINGING SEGMENT 4 LENGTH 5 POSITION 43
     the 
    INFRINGING SEGMENT 5 LENGTH 5 POSITION 54
     the 
    INFRINGING SEGMENT 6 LENGTH 3 POSITION 15
     fo
    
    CASE 2
    INFRINGING SEGMENT 1 LENGTH 4 POSITION 0
    xyzz
    INFRINGING SEGMENT 2 LENGTH 3 POSITION 7
    zzy
    INFRINGING SEGMENT 3 LENGTH 2 POSITION 10
    y