| UDESC Selection Contest 2024-1 |
|---|
| Закончено |
Lívia is the captain of a spaceship that was attacked by space pirates, and, as a last resort for survival, she had to eject herself from the ship.
Now, Lívia finds herself floating aimlessly through space, and the only things she has with her are her cellphone and a pair of headphones. Luckily, she managed to send an SOS before leaving the ship and knows that, although it may take a few days, help is on the way. To pass the time, she decided to listen to some songs by her favorite artist, Saylor Twift.
Lívia created her own musical notation for the melodies she listens to. She considers that a song contains at most 26 distinct musical notes, and, when listening to a melody, she represents each musical note by one of the letters of the Latin alphabet (from "a" to "z"). Therefore, a melody is represented by a sequence of letters.
Additionally, Lívia wrote down $$$N$$$ pairs of notes $$$(u, v)$$$ to represent that note $$$v$$$ sounds good after note $$$u$$$. Now, she wants to compile a playlist with all of Saylor Twift's beautiful songs, so she asked for your help. Lívia considers beautiful only the melodies where, for every note played (except the first), it sounds good after the previous note.
Lívia emphasizes that a certain note $$$v$$$ sounding good after note $$$u$$$ does not mean that note $$$u$$$ would also sound good after note $$$v$$$. Given a melody by Saylor Twift, determine whether Lívia will consider it beautiful or not.
The first line of the input contains a string $$$S$$$ $$$(1 \le |S| \le 10^5)$$$, representing Saylor Twift's song.
The second line contains an integer $$$N$$$ $$$(0 \le N \le 676)$$$, the number of pairs of musical notes that sound good together.
Each of the next $$$N$$$ lines contains two Latin alphabet letters, $$$u$$$ and $$$v$$$, representing that note $$$u$$$ sounds good before note $$$v$$$.
The output must contain "SIM" if Lívia will consider the melody beautiful, or "NAO" if she will not.
abac3a bb aa c
SIM
abac3a bc ab a
NAO
taylorswift10l oa yy li fo rr ss ww it af t
SIM
cde2c dc e
NAO
| Название |
|---|


