【数组模拟链表】UVA

来源:互联网 发布:汽车零部件进出口数据 编辑:程序博客网 时间:2024/06/07 14:03

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");    }}