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 - &quot;Accordian&quot; Patience POJ 1214 链表题解的更多相关文章

  1. 【习题 6-9 UVA - 127】"Accordian" Patience

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...

  2. 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 ...

  3. ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  4. UVa 127 - "Accordian" Patience

    题目:52张扑克,从左到右在平面上排列,按着如下规则处理: 1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面. 2.如果可以移动到多个位置,移动到最左端的牌上面. ...

  5. UVA 11732 &quot;strcmp()&quot; Anyone? (Trie)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. Cookie rejected: Illegal path attribute &quot;/nexus&quot;. Path of origin: &quot;/content/&quot; 解

    问题叙述性说明 通过运行"mvn clean deploy" 命令 将 Maven 项目公布 Nexus 当PW.举例控制台输出以下警告消息: [INFO] Downloaded: ...

  7. socket何时处于”读就绪状态“?---通过“应用程序大爷&quot;和&quot;内核孙子&quot;对话再看重要的select函数的使用方法

    前面. 我已经陆续介绍过select函数的一些零碎知识, 在本文中,我们来讨论这样一个问题:socket何时处于读就绪状态? 事实上主要讨论select函数, 毕竟socket的读就绪状态会导致sel ...

  8. 2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

    这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小 ...

  9. WAMP 2.5 &quot;FORBIDDEN&quot; error

    对于web开发人员来说.远程訪问站点能够非常方便的提高开发站点开发效率,那么在wamp环境下,默认仅仅支持本地訪问,那么怎样訪问开启远程站点訪问呢? 开启方法: wamp2.5(32bit) 集成环境 ...

随机推荐

  1. 13-JS中的面向对象

    创建对象的几种常用方式 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 1.使用Object或对象字面量创建对象 JS中最基本创建对象的 ...

  2. GitHub上README.md的简单介绍

    1.编辑README文件 大标题(一级标题):在文本下面加等于号,那么上方的文字就变成了大标题,等于号的个数无限制,但一定要大于0 大标题 ==== 中标题(二级标题):在文本下面加下划线,那么上方的 ...

  3. ipython+notebook使用教程(转载)

    ipython是python交互环境的增强版 IPython notebook目前已经成为用Python做教学.计算.科研的一个重要工具.IPython Notebook使用浏览器作为界面,向后台的I ...

  4. 性能测试五:jmeter进阶之后置处理器(正则、json提取器)

    如,从get返回的json中提取stock的值 作为post的请求参数 1.JSON提取器 专门对json数据进行提取的后置处理器 Debug Sampler:记录之前的请求的所有参数及数据 2.正则 ...

  5. pytest一:pytest 框架介绍

    pytest 是 python 的一种单元测试框架,与python 自带的 unittest测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高.根据pytest 的官方网站介绍,它具 ...

  6. 算法-----python实现

    斐波那契数列 def f(n): if n == 1: return 1 elif n == 2: return 1 else: return f(n-1)+f(n-2) print(f(8)) 用普 ...

  7. Mac下Eclipse读取不到环境变量

    问题: 用Eclipse时候读取不到 ~/.bash_profile 下定义的环境变量,确切的说,是GUI应用读取不到终端的(如eclipse) 解决: 1,下载启动代理器: curl https:/ ...

  8. hdu 2197 求长度为n的本原串 (快速幂+map)

    Problem Description由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,10 ...

  9. ubuntu16.04LTS服务器,python2.7升级到3.6,且同时升级pip

    ubuntu这个服务器,默认是带了python2.7和python3.5版本的. 如果想升级到python3.6,怎么办? 我综合了网上的几个帖子,在几台服务器上测试了一下, 没什么大问题,作个记录. ...

  10. Jquery框架1.选择器|效果图|属性、文档操作

    1.JavaScript和jquery的对比 书写繁琐,代码量大 代码复杂 动画效果,很难实现.使用定时器 各种操作和处理 <!DOCTYPE html> <html lang=&q ...