Showstopper
Sharing is caring! This weekend, the RSP graduates are visiting the The University of Adelaide to impart their computer science knowledge.
You've managed to book out lecture halls, numbered from
to
, which can support two lectures each. There are
graduates who want to give a single lecture about a particular topic, but each graduate can only access some subset of room numbers. Additionally, to keep things interesting, two lectures cannot use the same lecture hall if their topics are rotationally equivalent.
A string
can be rotated by moving some number of characters from the start of the string to the end, keeping their original order. For example, rotating
HELLOWORLDbypositions produces
WORLDHELLO. Two strings are considered rotationally equivalent if one can be obtained from the other in this way.
What is the maximum number of lectures you are able to host at the university?
Input
The first line contains two integers, and
,
, the number of lecture halls, and the number of lectures the students want to deliver.
The next lines contain a string
, an integer
, and then
more integers
, where
represents the topic for one graduate's lecture,
is the number of rooms they have access to, and
are the room numbers they have access to.
Output
Output the maximum number of lectures that can be delivered.
Example
Input 1
3 6
HOWTORSP 2 1 2
RSPHOWTO 2 2 3
FLOWCUT 2 1 3
CUTFLOW 2 1 3
INTROTOAUCPL 1 1
OUTROTOAUCPL 1 3
Output 1
5
There are lecture halls and
students who want to do the following lectures:
Rotationally equivalent to HOWTORSP:
HOWTORSP, in roomor
.
RSPTOHOW, in roomor
.
Rotationally equivalent to FLOWCUT:
FLOWCUT, in roomor
.
CUTFLOW, in roomor
.
Rotationally equivalent to INTROTOAUCPL:
INTROTOAUCPL, in room.
Rotationally equivalent to OUTROTOAUCPL:
OUTROTOAUCPL, in room.
We can do the following allocation:
- Room
:
HOWTORSPandFLOWCUT - Room
:
RSPTOHOWonly. - Room
:
INTROTOAUCPLandCUTFLOW OUTROTOAUCPLcan't be ran.
Therefore, a total of lectures can be ran. It can be shown that this is the maximum number for this case.
Input 2
4 9
CAB 2 1 2
AB 2 1 2
BA 2 3 4
CAB 2 1 2
BA 2 3 4
BCA 2 1 2
UNIQUE 4 1 2 3 4
AB 2 2 3
ABC 4 1 2 3 4
Output 2
8
Input 3
3 5
AAA 3 1 2 3
AAA 3 1 2 3
AAA 3 1 2 3
AAA 3 1 2 3
AAA 3 1 2 3
Output 3
3
Comments