Codeforces Round 130 (Div. 2)
B
Solitaire
Codeforces
1900
原题
← AC →
翻译中…

A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules:

翻译中…
  • A deck of $*n*$ cards is carefully shuffled, then all $*n*$ cards are put on the table in a line from left to right;
  • Before each move the table has several piles of cards lying in a line (initially there are $*n*$ piles, each pile has one card). Let's number the piles from left to right, from 1 to $*x*$. During one move, a player can take the whole pile with the maximum number $*x*$ (that is the rightmost of remaining) and put it on the top of pile $*x* - 1$ (if it exists) or on the top of pile $*x* - 3$ (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile $*x*$ goes on top of pile $*y*$, then the top card of pile $*x*$ becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1;
  • The solitaire is considered completed if all cards are in the same pile.
翻译中…

Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not.

翻译中…

Input

翻译中…

The first input line contains a single integer $*n*$ $(1 ≤ *n* ≤ 52)$ — the number of cards in Vasya's deck. The next line contains $*n*$ space-separated strings $*c*_{1}, *c*_{2}, ..., *c*_{*n*}$, where string $*c*_{*i*}$ describes the $*i*$-th card on the table. Each string $*c*_{*i*}$ consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right.

翻译中…

A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C".

翻译中…

It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat.

翻译中…

Output

翻译中…

On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise.

翻译中…

Note

翻译中…

In the first sample you can act like that:

翻译中…
  • put the 4-th pile on the 1-st one;
  • put the 3-rd pile on the 2-nd one;
  • put the 2-nd pile on the 1-st one.
翻译中…

In the second sample there is no way to complete the solitaire.

翻译中…

样例

Input #1
4
2S 2S 2C 2C
Output #1
YES
InputCopy OutputCopy
Input #2
2
3S 2C
Output #2
NO
InputCopy OutputCopy
题目信息
题号
208B
时间限制
2 s
内存限制
256 MB
标签
DFS 及类似
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。