《链表》C++中链表的几种表现情况(以Broken Keyboard (a.k.a. Beiju Text) UVA - 11988 为例说明)

总论

链表作为一种基础的数据结构,在历来的比赛试题中层出不穷。与只对应的就是vector。两者的区别在于:vector是一片连续的内存,查询快而更改慢,list通常是一段不连续的内存,用指针连接,查询慢而更改快。一般来说,c++的链表据我所了解目前应该有3种应用方式。
1.用数组模拟指针的应用过程;
2.STL list;
3.自己从底层实现其数据结构。

具体例题

You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed (internally).

You’re not aware of this issue, since you’re focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).

In Chinese, we can call it Beiju. Your task is to find the Beiju text.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each case, print the Beiju text on the screen.

Example 1

扫描二维码关注公众号,回复: 12064429 查看本文章

Input example #1
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Output example #1
BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

数组模拟AC代码展示

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<math.h>
#define maxn 100005

using namespace std;

int main(){
    
    
    //本题为链表知识。
    string s;
    while(cin>>s)
    {
    
    
        int len=s.length();
        char str[len+1];
        for(int i=1;i<=len;i++)//next[0]指向第一个元素,因而字符串首位不能有元素对应
            str[i]=s[i-1];

        int*next=new int[len+1]{
    
    };//目前字母跟着的下一个字母 指针需要有一个头指针
        int cur=0;//现在坐标
        int last=0;//最后坐标

        for(int i=1;i<=len;i++)
        {
    
    
            if(str[i]=='[')
                cur=0;
            else if(str[i]==']')
                cur=last;
            else{
    
    
                    //插入元素步骤,第一步插入元素的next=原元素的next,
                    //第二步原元素的next变为插入元素。
                    next[i]=next[cur];
                    next[cur]=i;
                    if(last==cur) last=i;
                    cur=i;
            }
        }

        for(int i=next[0];i!=0;i=next[i])
                cout<<str[i];
        cout<<endl;

        delete [] next;
    }

    return 0;
}

如代码所示,next[]数组起到的就是指针的作用,cur,目前位置,last 最后一个位置。

STL listAC代码展示

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<math.h>
#define maxn 100005

using namespace std;


int main(){
    
    

    //list STL
    string s;
    while(cin>>s)
    {
    
    
        int len=s.length();
        list<char>ls;
        list<char>::iterator it=ls.begin();

        for(int i=0;i<len;i++)
        {
    
    
            if(s[i]=='[')
                it=ls.begin();
            else if(s[i]==']')
                it=ls.end();
            else{
    
    
                    //法一:
                    /*
                    it=ls.insert(it,s[i]);//加入元素,到it指向的地方。 返回值是指向str[i]的iterator,所以需要++
                    it++;
                    */
                    //法二:
                    ls.insert(it,s[i]);
            }
        }


        for(it=ls.begin();it!=ls.end();it++)
                cout<<*it;
        cout<<endl;
        ls.clear();
    }

    return 0;
}

对于STL list的insert函数的返回值有一个很好的解释博客:STL list 的insert()和erase()
这也是两种思路的来源。在这里我还要推荐一篇博客:c++STL list用法总结
里面对于STL list的用法总结较为全面,这里不再详述。

List底层代码实现。

这个一般打比赛的话,很少用,也较为复杂,过段时间闲时补上。(大概的原理,结构体,next指针,数据表示等)

猜你喜欢

转载自blog.csdn.net/Look_star/article/details/107027525