A8600 | Solitaire
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
- 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.
输入格式
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.
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.
- 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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted