CF208B.Solitaire
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
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 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.
一个名叫瓦夏的男孩想玩一种古老的俄罗斯纸牌游戏,叫做“手风琴”。在该游戏中,玩家必须遵守以下规则:
- 一副包含 n 张牌的牌组被仔细洗匀,然后将全部 n 张牌从左到右依次排放在桌面上;
- 每次操作前,桌面上有若干堆牌(初始时共有 n 堆,每堆仅含一张牌)。我们从左到右将这些牌堆编号为 1 到 x。在一次操作中,玩家可以取走编号最大的那堆(即当前剩余牌堆中最右侧的一堆,编号为 x),并将其整体叠放到第 x−1 堆(若存在)的顶部,或叠放到第 x−3 堆(若存在)的顶部。玩家仅当两堆牌顶牌的花色相同或点数相同时,才可将一堆牌叠放到另一堆牌的顶部。请注意:若将第 x 堆叠放到第 y 堆之上,则第 x 堆的顶牌将成为新合成牌堆的顶牌。此外,每次操作都会使牌堆总数减少 1;
- 当所有牌最终都位于同一堆中时,该纸牌游戏视为完成。
瓦夏已经将牌洗好并摆放在桌面上,请你帮助他判断:是否有可能完成该游戏?
输入格式
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.
第一行输入包含一个整数 n(1≤n≤52),表示瓦西娅牌组中的牌数。
下一行包含 n 个用空格分隔的字符串 c1, c2, …, cn,其中字符串 ci 描述桌面上第 i 张牌。每个字符串 ci 恰好由两个字符组成:第一个字符表示该牌的点数,第二个字符表示其花色。桌面上的牌从左到右依次编号。
一张牌的点数由以下字符之一表示:“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“T”、“J”、“Q”、“K”、“A”;
一张牌的花色由以下字符之一表示:“S”、“D”、“H”、“C”。
不能保证该牌组包含所有可能的牌。此外,瓦西娅的牌组中可能出现重复的牌。
输出格式
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.
在单行中输出问题的答案:如果可以完成该纸牌游戏,则输出字符串 “YES”(不含引号);否则输出字符串 “NO”(不含引号)。
输入输出样例
输入#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.
在第一个样例中,你可以按如下方式操作:
- 将第 4 堆放到第 1 堆上;
- 将第 3 堆放到第 2 堆上;
- 将第 2 堆放到第 1 堆上。
在第二个样例中,无法完成该纸牌游戏。
输入解题思路,AI测评打分。不知道怎么写?