CF208B.Solitaire

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn cards is carefully shuffled, then all nn 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 nn piles, each pile has one card). Let's number the piles from left to right, from 1 to xx . During one move, a player can take the whole pile with the maximum number xx (that is the rightmost of remaining) and put it on the top of pile x1x-1 (if it exists) or on the top of pile x3x-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 xx goes on top of pile yy , then the top card of pile xx 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.

输入格式

The first input line contains a single integer nn (1<=n<=52)(1<=n<=52) — the number of cards in Vasya's deck. The next line contains nn space-separated strings c1,c2,...,cnc_{1},c_{2},...,c_{n} , where string cic_{i} describes the ii -th card on the table. Each string cic_{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.

输出格式

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.

输入输出样例

  • 输入#1

    4
    2S 2S 2C 2C
    

    输出#1

    YES
    
  • 输入#2

    2
    3S 2C
    

    输出#2

    NO
    

说明/提示

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.

首页