# UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

+关注继续查看

UVa和POJ都有这道题。

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()
{
while (true)
{
Node *it = new Node;
it->a = getchar();
if (it->a == '#') break;
it->b = getchar();
getchar();

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;
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)
int piles = 0;
while (it)
{
piles++;
it = it->post;
}
if (piles == 1) printf("%d pile remaining:", piles);
else printf("%d piles remaining:", piles);
while (it)
{
printf(" %d", it->size);
it = it->post;
}
putchar('\n');
}//while (true)
return 0;
}

P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组(poj3261)
P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组(poj3261)
6 0
LeetCode每日1题--左旋转字符串
LeetCode每日1题--左旋转字符串
18 0

30 0
HDU-2612，Find a way（BFS）
HDU-2612，Find a way（BFS）
35 0

1149 0
poj 1154 letters （dfs回溯）
http://poj.org/problem?id=1154 #include using namespace std; int bb[26]={0},s,r,sum=1,s1=1; char aa[25][25]; int dir[4][2]={-1,0,1,0,0,-1,0,1};...
630 0
【HDU 5510 Bazinga】字符串
2015沈阳区域赛现场赛第2题 题目链接：http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意：给定一个由字符串组成的序列，一共n个元素，每个元素是一个不超过2000个字符的字符串。求"存在秩小于 i 且不是 i 的子串"的最大的 i (1
840 0
+关注