UVa 127 - "Accordian" Patience POJ 1214 链表题解
UVa和POJ都有这道题。
不同的是UVa要求区分单复数,而POJ不要求。
使用STL做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错。
调试起来还是很费力的
本题理解起来也是挺费力的,要搞清楚怎样模拟也不easy啊,读题要非常细致。
纯指针的操作挺快的吧。
只是POJ 0ms,而UVa就0.2左右了。
三相链表:
1 仅仅要有叠起来的牌。那么就使用一个down指针指向以下的牌就能够了。
2 使用双向链表,能够方便前后遍历。
3 记得有了更新牌之后。又要又一次開始检查是否须要更新牌,这是模拟的须要。不然或WA的。
#include <stdio.h> struct Node
{
int size;
Node *pre, *post;
Node *down;
char a, b;
Node () : pre(NULL), post(NULL), down(NULL), size(1) {}
}; //insert n to m position
inline void insertNode(Node *&m, Node *&n)
{
n->post = m->post;
n->pre = m->pre;
if (m->post) m->post->pre = n;//小心断链
if (m->pre) m->pre->post = n;//小心断链
} inline void takeoutNode(Node *&n)
{
if (n->down)
{
Node *down = n->down;
insertNode(n, down);
return;
}
if (n->pre) n->pre->post = n->post;
if (n->post) n->post->pre = n->pre;
} inline void inStackNode(Node *&m, Node *&n)
{
n->size = m->size+1;
insertNode(m, n);
n->down = m;
} inline bool checkMovable(Node *n, Node *m)
{
return n->a == m->a || n->b == m->b;
} inline void pre3(Node *&n)
{
if (n->pre) n = n->pre;
if (n->pre) n = n->pre;
if (n->pre) n = n->pre;
} inline void pre1(Node *&n)
{
if (n->pre) n = n->pre;
} inline void deleteNodes(Node *&n)
{
while (n)
{
Node *p = n->post;
while (n)
{
Node *d = n->down;
delete n; n = NULL;
n = d;
}
n = p;
}
} int main()
{
Node *head = new Node; //Dummy head
while (true)
{
Node *it = new Node;
it->a = getchar();
if (it->a == '#') break;
it->b = getchar();
getchar(); head->post = it;//initialize chain
it->pre = head; for (int i = 1; i < 52; i++)
{
Node *p = new Node;
p->a = getchar();
p->b = getchar();
getchar();
it->post = p;
p->pre = it;
it = p;
}
bool checkMove = true;
while (checkMove)
{
checkMove = false;
it = head->post;
while (it)
{
Node *post = it->post;
Node *p = it;
pre3(p);
if (p && p != head && checkMovable(p, it))
{
checkMove = true;
takeoutNode(it);
inStackNode(p, it);//调用參数不要乱了
break;
}
p = it;
pre1(p);
if (p && p != head && checkMovable(p, it))
{
checkMove = true;
takeoutNode(it);
inStackNode(p, it);
break;//题意,理解
}
it = post;
}//while (it)
}//while (checkMove && piles > 1)
it = head->post;
int piles = 0;
while (it)
{
piles++;
it = it->post;
}
if (piles == 1) printf("%d pile remaining:", piles);
else printf("%d piles remaining:", piles);
it = head->post;
while (it)
{
printf(" %d", it->size);
it = it->post;
}
putchar('\n');
deleteNodes(head->post);
}//while (true)
delete head;
return 0;
}
UVa 127 - "Accordian" Patience POJ 1214 链表题解的更多相关文章
- 【习题 6-9 UVA - 127】"Accordian" Patience
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...
- Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟
Input Input data to the program specifies the order in which cards are dealt from the pack. The inpu ...
- ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- UVa 127 - "Accordian" Patience
题目:52张扑克,从左到右在平面上排列,按着如下规则处理: 1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面. 2.如果可以移动到多个位置,移动到最左端的牌上面. ...
- UVA 11732 "strcmp()" Anyone? (Trie)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Cookie rejected: Illegal path attribute "/nexus". Path of origin: "/content/" 解
问题叙述性说明 通过运行"mvn clean deploy" 命令 将 Maven 项目公布 Nexus 当PW.举例控制台输出以下警告消息: [INFO] Downloaded: ...
- socket何时处于”读就绪状态“?---通过“应用程序大爷"和"内核孙子"对话再看重要的select函数的使用方法
前面. 我已经陆续介绍过select函数的一些零碎知识, 在本文中,我们来讨论这样一个问题:socket何时处于读就绪状态? 事实上主要讨论select函数, 毕竟socket的读就绪状态会导致sel ...
- 2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)
这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小 ...
- WAMP 2.5 "FORBIDDEN" error
对于web开发人员来说.远程訪问站点能够非常方便的提高开发站点开发效率,那么在wamp环境下,默认仅仅支持本地訪问,那么怎样訪问开启远程站点訪问呢? 开启方法: wamp2.5(32bit) 集成环境 ...
随机推荐
- Oracle数据库操作基本语法
创建表 SQL>create table classes( classId number(2), cname varchar2(40), birthda ...
- Ex 6_14 布料剪裁问题_第八次作业
子问题定义: 定义p[i][j]为布料宽为i,高为j的最优产出,每次剪下一块布料,剩余布料最多形成三块矩阵面料.每次剪裁会有两种情况,水平切割布料,其次是将布料旋转90度后在切割布料. 递归关系: 初 ...
- AndroidStudio中builde.gradle文件详解
Android Studio是采用Gradle来构建项目的,一个Android项目中包含两个build.gradle文件,如下图: (1)最外层目录下build.gradle文件 最外层目录下 ...
- Linear Algebra(未完待续)
[矩阵消元] The result of multiplying a matrix by some vector is a combination of the columns of the matr ...
- python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)
一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...
- java select 多字段处理查询结果辅助类
接口 ResultSetMetaData 可用于获取关于 ResultSet 对象中列的类型和属性信息的对象 使用示例: ResultSet rs = stmt.executeQuery(" ...
- MVC开发中的常见错误-03-System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。
return Db.SaveChanges()>0; return CurrentDBSession.SaveChanges(); RoleInfoService.EditEntity(role ...
- 基于Linux平台的自动化运维Devops-----之自动化系统部署
一.自动化运维的背景网站业务上线,需要运维人员在短时间内完成几百台服务器部署,包括系统安装.系统初始化.软件的安装与配置.性能的监控......所谓运维自动化,即在最少的人工干预下,利用脚本与第三方工 ...
- 浅析C语言的变量
参考资料 寄存器变量 用register声明的变量是寄存器变量,是存放在CPU的寄存器里的.而我们平时声明的变量是存放在内存中的.虽说内存的速度已经很快了,不过跟寄存器比起来还是差得远. 寄存器变量和 ...
- MyBatis之Hello world(Mybatis入门)
MyBatis中文网,超详细的:http://www.mybatis.org/mybatis-3/zh/index.html MyBatis英文网:http://www.mybatis.org/myb ...