例题6-4 破损的棋盘 uva 11988

这道例题涉及了一些单向链表的用法,
第一,单向链表在记录的时候,只考虑他后面是谁,那么第一个数怎么办呢,
通过虚设初始 next[0] 来存第一位,而什么时候在最后一位即nex[pos] = 0的时候
第二,数字字母需要学会看成一题,即在写链表的时候,用数字代替字母

代码如下

#include <bits/stdc++.h>
using namespace std;
const int MA  = 1e5 + 5;
char s[MA];
int nex[MA];
int main()
{
	while (scanf("%s",s+1) == 1)
	{
		memset(nex,0,sizeof(nex));
		int n = strlen(s+1),pos = 0,wend = 0,k = 0;
		//pos表示位置,wend表示末位对应的数字 
		for (int i = 1; i <= n; i++)
		{
			if(s[i] == '[') pos = 0;
			else if(s[i] == ']') pos = wend;
			else{
				nex[i] = nex[pos];
				nex[pos] = i;//它的下一位
				pos = i;
				if (nex[pos] == 0) wend = pos;	
			}
		}
		while(nex[k])
		{
			printf("%c",s[nex[k]]);	
			k = nex[k] ;
		}
		printf("\n"); 
	}	
} 
发布了55 篇原创文章 · 获赞 1 · 访问量 2640

猜你喜欢

转载自blog.csdn.net/qq_37548017/article/details/102805533