University of Lethbridge - Mathematics & Computer Science

Problem #2 One Set, Two Set, Red Set, Blue Set

Prompt for two test files, each containing up to 100 lines of single strings with each string from 1 to 32 alphabetic characters long with no leading, trailing, or embedded spaces. The contents of each file is a classical SET OF VALUES, meaning that each line is one value in the set, there are no duplicate lines within a specific file, and the lines in the file have no specific order. The problem is to compute four output sets:

  1. The intersection of the two sets A and B (A the first file, B the second).
  2. The union of the two sets.
  3. The difference of A minus the common elements of B.
  4. The difference of B minus the common elements of A.
The output data is to be written to set.out, with one left justified element per line with no leading or trailing blanks. In all cases the order of the elements in the output file must be the same as in the original files, and where elements of both A and B are present, all elements of A must be printed before the B elements. The output format should be:

BEGIN INTERSECTION
     data lines
END INTERSECTION
BEGIN UNION
     data lines
END UNION
BEGIN AMINUSB
     data lines
END AMINUSB
BEGIN BMINUSA
     data lines
END BMINUSA

where the data lines are indented five spaces.

Sample Input :

INPUT FILE 1
abababababababababababababababa
thisisalsoadataline
rdcuyikkjkjih
ababababababab

INPUT FILE 2
rdcuyikkjkjih
ababababababababa
poiyutyr
thisisalsoadataline
andthisisanother

Output File:

BEGIN INTERSECTION
     thisisalsoadataline
     rdcuyikkjkjih
END INTERSECTION
BEGIN UNION
     abababababababababababababababa
     thisisalsoadataline
     rdcuyikkjkjih
     ababababababab
     ababababababababa
     poiyutyr
     andthisisanother
END UNION
BEGIN AMINUSB
     abababababababababababababababa
     ababababababab
END AMINUSB
BEGIN BMINUSA
     ababababababababa
     poiyutyr
     andthisisanother
END BMINUSA

Original problem: Mountain Region ACM contest, 1994