Problem Description
输入一行文本,如果遇到’[‘就会跳到最前面继续输入,如果遇到’]’就会跳到最后面继续输入
代码:数组模拟链表的后面插入
#include<cstdio>
#include<cstring>
using namespace std;
char s[100005];//数据数组
int next[100005];//指向数组
int main()
{
int i, last, cnr, len;
while(~scanf("%s", s + 1))
{
cnr = last = 0;
len = strlen(s + 1);
next[0] = 0;
for(i = 1; i <= len; i++)
{
if(s[i] == '[') cnr = 0;
else if(s[i] == ']') cnr = last;
else//核心,和前向星很相似的思维
{
next[i] = next[cnr]; //记录上一个点的,的下一个的下标
next[cnr] = i;//上一个点指向这一个点
if(cnr == last) last = i;
cnr = i;
}
}
for(i = next[0]; i != 0; i = next[i])
printf("%c", s[i]);
printf("\n");
}
}