``Accordian'' Patience 

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 pile remaining: 52

解决方式:

首先题意要细致理解,然后模拟就可以,只是,因为处理大量的字符,必须用scanf来输入,并且scanf不能对string类型进行输入,所以不能用string,要用字符数组,假设用string,就必须把字符赋给string,这样非常耗时,因为这个,这里tle了几次。然后是模拟,用vector,要2000多毫秒,用list可能比較少点。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<stack>
using namespace std;
struct node
{
char x,y;
};
bool cmp(node a,node b)
{
if(a.x==b.x) return true;
if(a.y==b.y) return true;
return false;
}
int main(){
while(1)
{
vector<stack<node > >card;//定义vector(容器),包括的元素类型为stack<node>;
char temp[3];
scanf("%c",&temp[0]);
if(temp[0]=='#') break;
scanf("%c",&temp[1]);
stack<node > p;
node c;
c.x=temp[0];c.y=temp[1];
p.push(c);
card.push_back(p);//把栈存入card容器。
for(int i=1;i<52;i++)
{
getchar();
scanf("%c%c",&temp[0],&temp[1]);
node c;
c.x=temp[0];c.y=temp[1];
stack<node > t;
t.push(c);
card.push_back(t);
}
/* int len=card.size();
for(int i=0;i<len;i++)
{
cout<<card[i].top()<<endl;
}
getchar();*/
int len=card.size();//求容器的大小
for(int i=0;i<len;i++)
{
int j=i; if(j-3>=0){
if(cmp(card[j-3].top(),card[j].top())){ //容器也可像数组一样通过下标来使用。
card[j-3].push(card[j].top());
card[j].pop();
if(card[j].empty())
{
card.erase(card.begin()+j);//删除某个位置的栈,參数必须为指向该位置的指针
len=card.size();
}
i=-1;
continue;
} }
if(j-1>=0){
if(cmp(card[j-1].top(),card[j].top())){
card[j-1].push(card[j].top());
card[j].pop();
if(card[j].empty())
{
card.erase(card.begin()+j);
len=card.size();
}
i=-1;
continue;
}
} }
int l=card.size();
if(l>1)
{
printf("%d piles remaining:",l);
for(int i=0;i<l;i++)
printf(" %d",card[i].size());
}
else {
printf("%d pile remaining:",l); printf(" %d",card[0].size());
}
printf("\n");
getchar();
} return 0;
}

``Accordian&#39;&#39; Patience的更多相关文章

  1. 百度UEditor组件出现Parameters: Invalid chunk &#39;&#39; ignored警告的分析

    使用百度UEditor在线编辑器组件时,出现Parameters: Invalid chunk '' ignored的警告,之前的项目使用却没有.两个项目的环境应该是一样的. 没有时间去对照两项目使用 ...

  2. Could not find class &#39;****&#39;, referenced from method #####

    找不到类,多半也是和第三方的jar包有关. 将找不到的类.在下图中的地方勾选出来.假设jar太多.有的类有冲突的话,须要明白其先后顺序. 请外一篇和第三方jar有关的异常的文章. Conversion ...

  3. mysql 1449 : The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist 解决方法

    权限问题,授权 给 root  全部sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...

  4. 佩特来项目经验小集合(2)___组合查询存储过程,报错 &quot;varchar JBID=&#39;&#39; 转换成数据类型 int 时失败&quot;

       今天写一个组合查询的存储过程遇到这样一个问题:在将 varchar 值 'SELECT * FROM View_DLS_WXJD_Customer WHERE 1=1 and JBID ='' ...

  5. warning: directory not found for option &#39; &#39;

    解决: 选择项目名称-->Targets-->Build Settings-->Search Paths-->Library Search Paths 删除相应路径

  6. Grant的时候报错的解决:Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)

    mysql> grant all on *.* to 'root'@'192.168.1.1' identified by 'password'; ERROR 1045 (28000): Acc ...

  7. Suse发生了错误Access denied for user &#39;&#39;@&#39;localhost&#39; to&amp;

    好久没实用MySQL了,上次由于装了Banq的论坛系统.在用MySQL Administrator进去的时候居然提示mysql error number 1045 access denied for ...

  8. [mysql]刷新windows恢复后mysql和&quot;Access denied for user&#39;root&#39;@&#39;IP&#39;&quot;处理问题

    mysql数据库软件实际上是绿色的,重装系统后能够继续使用. 1.重装系统保留原有的后mysql安装文件夹,数据文件夹. 2.制作用于启动一个批处理文件mysql:[run.bat]的文件存储在mys ...

  9. Android学习----异常(2):Please ensure that adb is correctly located at &#39; ... &#39;

    打开任务管理器,在后台进程中找到 kadb,结束这个进程,重新启动eclipse.

随机推荐

  1. Python3的基本数据类型及常用的方法

    python3的基本数据类型: 在python3当中有这么几种基本的数据类型:int(整形).str(字符串).list(列表).tuple(元组).dict(字典).bool(布尔值)等.数字整体划 ...

  2. Oracle跟踪分析数据库启动的各个阶段

    目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...

  3. Debian7配置LAMP(Apache/MySQL/PHP)环境及搭建建站

    完整Debian7配置LAMP(Apache/MySQL/PHP)环境及搭建建站 第一.安装和配置Apache Web服务器 运行升级命令来确保我们的系统组件各方面都是最新的. apt-get upd ...

  4. [luoguP3668] [USACO17OPEN]Modern Art 2 现代艺术2(栈)

    传送门 还是一个字——栈 然后加一大堆特判 至少我是这么做的 我的代码 #include <cstdio> #include <iostream> #define N 1000 ...

  5. [HNOI2010]CHORUS 合唱队 (区间DP)

    题目描述 对于一个包含 NN 个整数的数列 AA ,我们可以把它的所有元素加入一个双头队列 BB . 首先 A1A1 作为队列的唯一元素,然后依次加入 A2∼ANA2∼AN ,如果 Ai<Ai− ...

  6. 做运动(Dijkstra+并查集+MST)

    上面的题解是这样,这道题我真的脑残,其实打代码的时候就意识到了许多,可以用Dfs+Dij+二分,这样还可以卡一卡 但是我打了spfa+spfa+二分,这个显然很慢,类似的题目我好像还做过一道的,就是在 ...

  7. 【前端学习笔记】ajax与php之间的互动

    ajax通常会牵扯到跨域问题,所以我们通常的解决方案是,通过ajax将参数传到后台php文件中 在后台通过php文件进行跨域访问api,再将结果返回到ajax响应中.需要注意一下几点: 1.可以通过& ...

  8. writeValueAsString封装成工具类

    封装成工具类 <span style="font-family:Microsoft YaHei;">public static String toJsonByObjec ...

  9. Spring Boot SpringSecurity5 身份验证

    对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache Shiro.Spring Security). pom.xm ...

  10. 乘法运算(codevs 3254)

    题目描述 Description 编制一个乘法运算的程序. 从键盘读入2个100以内的正整数,进行乘法运算并以竖式输出. 输入描述 Input Description 输入只有一行,是两个用空格隔开的 ...