紫书例题6-4 UVa 11988 ( 链表

用数组模拟链表 ,以前只用过指针模拟,数组方便了不少啊
没什么好说的(不是很理解啊 先记录一下唉2

/*一直不知道我HOME与end作用 所以一直没有理解题意QAQ */ 
/* 题意就是只要出现[]包围的内容 就按照出现的顺序扔到句子最前面 */ 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
#define N 1000000 
const int mod = 1e9+7;
char str[N];
int step[N];           //定义next数组表示str的下标 
int last, cur;  // cur为光标位置, last 为最后一个字符位置 
int main()
{
    while(~scanf("%s",str+1)) {  //从s[1]输入 
        int len = strlen(str+1);
        last = cur = 0;
        step[0] = 0;               // 表示光标最开始在屏幕的最左边 
        for(int i = 1;i <= len; i++) {  
            if(str[i] == '[')      
                cur = 0;            //瞬间将光标移到最左边
            else if(str[i] == ']') 
                cur = last;         //括号内内容结束
            else {
                step[i] = step[cur];
                step[cur] = i;
                printf("%d %d\n",step[cur],cur);
                if(cur == last) last = i;         //要更新最后一个字符的编号啊 
                cur = i;                          //移动光标 
            } 
        }
        for(int i = step[0];i != 0; i = step[i]) {
            printf("%d ",i);
        }printf("\n");
        for(int i = step[0];i != 0; i = step[i]) {
            printf("%c",str[i]);
        }
        printf("\n");
    }

return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值