“Accordian” Patience
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1051 Accepted: 386

Description

You are to simulate the playing of games of ``Accordian’’ patience, the rules for which are as follows:

Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.
Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left, move it three positions.

Input

Input data to the program specifies the order in which cards are dealt from the pack. The input contains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the input file contains a # as its first character. Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).

Output

One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing ``Accordian patience’’ with the pack of cards as described by the corresponding pairs of input lines.

Sample Input

QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

Sample Output

6 piles remaining: 40 8 1 1 1 1
1 piles remaining: 52

Source

New Zealand 1989,UVA 127

问题链接:POJ1214 “Accordian” Patience
问题简述:(略)
问题分析
    模拟题,用STL的vector来实现,只是过程繁琐。需要嵌套vector,需要注意。也需要注意计算时间。
    UVA与POJ本质上是同一个题,只是输出结果时,单词"pile"的单数和复数形式用的不同。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(UVA)如下:

/* UVA127 "Accordian" Patience */#include <iostream>
#include <cstdio>
#include <vector>using namespace std;const int N = 52;
struct Card {char c[3];
};bool eq(Card& a, Card& b)
{return a.c[0] == b.c[0] || a.c[1] == b.c[1];
}int main()
{for(;;) {vector< vector<Card> > v;Card a;for(int i = 0; i < N; i++) {scanf("%s", a.c);if(a.c[0] == '#')return 0;vector<Card> s;s.push_back(a);v.push_back(s);for(int j = v.size()-1, k; j < (int)v.size(); j++) {Card c = v[j].back();for(k = j; k > 0; k--) {if(k >= 3 && eq(c, v[k-3].back())) {k = k - 2;continue;}if(!eq(c, v[k-1].back()))break;}if(k != j) {v[k].push_back(v[j].back());v[j].pop_back();if(v[j].empty())v.erase(v.begin() + j);}j = k;}}int len = v.size();if(len > 1)printf("%d piles remaining:", len);elseprintf("%d pile remaining:", len);for(int i = 0; i < len; i++)printf(" %d", v[i].size());printf("\n");}return 0;
}

AC的C++语言程序(POJ)如下:

/* POJ1214 "Accordian" Patience */#include <iostream>
#include <cstdio>
#include <vector>using namespace std;const int N = 52;
struct Card {char c[3];
};bool eq(Card& a, Card& b)
{return a.c[0] == b.c[0] || a.c[1] == b.c[1];
}int main()
{for(;;) {vector< vector<Card> > v;Card a;for(int i = 0; i < N; i++) {scanf("%s", a.c);if(a.c[0] == '#')return 0;vector<Card> s;s.push_back(a);v.push_back(s);for(int j = v.size()-1, k; j < (int)v.size(); j++) {Card c = v[j].back();for(k = j; k > 0; k--) {if(k >= 3 && eq(c, v[k-3].back())) {k = k - 2;continue;}if(!eq(c, v[k-1].back()))break;}if(k != j) {v[k].push_back(v[j].back());v[j].pop_back();if(v[j].empty())v.erase(v.begin() + j);}j = k;}}int len = v.size();printf("%d piles remaining:", len);for(int i = 0; i < len; i++)printf(" %d", v[i].size());printf("\n");}return 0;
}

POJ1214 UVA127 Accordian Patience【vector】相关推荐

  1. 【vector】模拟vector操作

    目录 题目描述 输入格式 输出格式 输入输出样例 说明/提示 代码 题目描述 给出一系列操作包括 1 x,表示将元素push_back到vector中 2 查询vector中元素个数 3 清空vect ...

  2. 【 MATLAB 】norm ( Vector and matrix norms )(向量范数以及矩阵范数)

    norm Vector and matrix norms Syntax n = norm(v) n = norm(v,p) n = norm(X) n = norm(X,p) n = norm(X,' ...

  3. 【集合】Vector 是线程安全的?

    1.美图 2.概述 今天遇到一个问题,让我怀疑Vector是否是线程安全的 [Flink]报错 KryoException ConcurrentModificationException StackO ...

  4. 【日常学习】【语法】STL之vector

    本周四学习了vector 由于时间关系拖到现在才发总结 那么现在总结一些vector的常用用法 vector需要有一个名字 即数组名 因为vector本身就是一个不定长数组 我们以a为例 那么相当于建 ...

  5. 【车载以太网】【测试】Vector测试方案

    Vector为广大用户提供车载以太网(TC8)测试解决方案,包括CANoe 12.0(以及Option Ethernet和相关硬件接口卡).vTESTstudio和VT System,如下图所示. 在 ...

  6. 【C++】vector类概述

    vector 一.标准库中的vector 1.1 vector的介绍 1.2 vector的常用接口 1.2.1 vector的常见构造 1.2.2 vector类对象的访问及遍历操作 1.2.3 v ...

  7. 【BZOJ4184】shallot 线段树+vector+线性基

    [BZOJ4184]shallot Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从 ...

  8. 【C++】手把手教你写出自己的vector类

    在上一篇博客中,我们学习了vector 的基本使用,以及 迭代器的失效问题: [C++]深入理解vector类(一) 今天我们来模拟实现以下vector类. 目录 成员变量 接口实现 构造函数 迭代器 ...

  9. C/C++动态开辟数组【C++:new/delete(推荐):int *arr = new int[m];】【C++:vector】【C:malloc() free()】

    一.C++的new/delete(推荐) 1.动态开辟一维数组 #include<iostream> #include<stdio.h>using namespace std; ...

最新文章

  1. iOS走近商城APP(四 runloop应用 获取通讯录并处理)
  2. (0027)iOS 开发之调整导航条上BarButtonItem与屏幕边界的间距
  3. 大连交通大学c语言考试题库,川哥的吩咐 (C语言代码)本着追大连交通大学的故事番,来简单解答一波!!!...
  4. [leetcode] Pow(x, n)
  5. 速学c++(1)-c++简介
  6. day_work_02
  7. java试题汇编_JAVA面试题汇编 - DotNet and J2EE Developer - BlogJava
  8. 计算机指令系统课件,计算机组成原理课件05指令系统.ppt
  9. 最好的计划是略有闲余的计划,用于缓冲必然出现的错误与突发事件(转)
  10. 剑指offer.从未到头打印链表
  11. ajax能拿到401axios无法拿到,解决axios.interceptors.response 401 403问题
  12. python遥感图像开发小软件_遥感影像深度学习标注软件的开发要点
  13. 3-产品经理学习笔记之产品经理的工作职责和能力模型
  14. 神经元细胞分布全身吗,人体神经细胞分布图
  15. Hbuilder里运行到手机或模拟器手机和电脑配置
  16. miui10android系统耗电,小米声势浩大的MIUI10 系统升级只是更换个主题并且更加耗电?...
  17. 【SAP Hana】X档案:SAP HANA SQL 基础教程
  18. Linux 文件系统与日志分析
  19. Linux shell实现阶乘
  20. loadrunner入门教程(33) -- Analysis图

热门文章

  1. 使用GDAL对HDF数据进行geoloc校正
  2. 现实给了梦想多少时间?
  3. Java中使用KCP协议
  4. linux c 数字变字符串,Linux C 知识 char型数字转换为int型 int型 转换为Char
  5. java的max函数比较三个数_java – 使用泛型创建返回较大函数的max函数
  6. scala的静态属性和静态方法
  7. android wchar t 中文,Android没有真正的wchar_t吗?
  8. SpringMVC测试框架(转载)
  9. 剑指offer面试题26. 树的子结构(链表)
  10. Qt总结之十四:uint8_t / uint16_t / uint32_t /uint64_t数据类型详解