码迷,mamicode.com
首页 > 编程语言 > 详细

UVa 11998 破碎的键盘(数组实现链表)

时间:2017-07-14 19:38:04      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:bit   bsp   ==   char   输出   开始   nbsp   home   color   

题意:

输入一行字符,其中包含‘[‘ 和 ‘]’, 意思为键盘上的home 和 end 键, 然后模拟字符在键盘上输入。 输入一行最终的结果

分析:

用数组模拟一个链表, 在链表的头尾插入字母然后输出即可, 方法需要多加练习才能练熟, 其实跟邻接表有点像。

为了方便起见,常常在链表的第一个元素之前放一个虚拟结点。

#include <bits/stdc++.h>
using namespace std;
const int maxl =  100000 + 7;
int main()
{
    
    char str[maxl];
    int cur, last;
    int next[maxl];//next[index] 就是str[index]的下一个字母是 str[next[index]]
    //为了方便起见,常常在链表的第一个元素之前放一个虚拟结点。 所以从str+1开始
    while(~scanf("%s", str+1))
    {
        int len = strlen(str+1);
        next[0] = 0;//一开始next指向虚拟节点
        cur = last = 0;
        for(int i = 1 ; i <= len; i++){
            if(str[i] == [){
                cur = 0;
            }
        //光标位于cur后面  cur...|
            else if(str[i] == ]){
                cur = last;
            }
            else{

                next[i] = next[cur];
                next[cur] = i;
                /*注意这两句 当cur = 0, i = 1时,
                next[1] = next[0] ------------ 把虚拟节点接到next[1]上
                next[0] = 1       ------------ 把0的下一个接为1
                这就实现了next0转移到了next1 next0 变为 1
                */
                if(cur == last) last = i;
                cur = i;
            }
        }

        for(int i = next[0]; i != 0; i = next[i]){
            printf("%c", str[i]);
        }
        puts("");
    }
}

 

UVa 11998 破碎的键盘(数组实现链表)

标签:bit   bsp   ==   char   输出   开始   nbsp   home   color   

原文地址:http://www.cnblogs.com/Jadon97/p/7171940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!