链表详解1

引入:链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到 O ( 1 ) O(1) O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是 O ( l o g n ) O(log n) O(logn) O ( 1 ) O(1) O(1)

这一段太玄学了,我放在这里只是让你在阅读此篇文章之前对链表有一个模糊的概念,对于之后的阅读有一定的帮助。

直接上例题:
[ UVa 11988 ]Broken Keyboard 破碎的键盘
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).

Output

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

Sample Input

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Sample Output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

题意简述
输入包含多组数据。每组数据占一行,包含不超过100000个字母、下划线、字符“ [ ”或者“ ] ”。其中字符“ [ ”表示光标由当前的位置到所有字符串的最前面(相当于电脑键盘上的home键),而“ ] ”表示光标由当前的位置到所有字符串的最后面(相当于电脑键盘上的end键)。输入结束标志为文件结束符(EOF)。

分析
最简单的想法自然是用数组来保存这段文本,然后用一个变量pos保存“光标位置”。这样,输入一个字符相当于在数组中插入一个字符(需要先把后面的字符全部右移,给新字符腾出位置)。
但是这样的代码会TLE 。why?因为每输入一个字符都可能会引起大量字符移动。在极端条件下,例如,2500000个a和“[”交替出现,则一共需要 0 + 1 + 2 + . . . + 2499999 = 6 ∗ 1 0 12 0 +1 +2 +... +2499999=6*10^{12} 0+1+2+...+2499999=61012次字符移动。
解决方案就是这篇文章的主角——链表。每输入一个字符就把它存起来,设输入字符串是s[1~n] ,则可以用next[i]表示在当前显示屏中s[i]右边的字符编号(即在s中的下标)。
为了方便起见,假设字符串s的最前面还有一个虚拟的s[0],则next[0]就可以表示显示屏中最左边的字符。再用一个变量cur表示光标位置:即当前光标位于s[cur]的右边。cur=0说明光标位于“虚拟字符”s[0]的右边,即显示屏的最左边。

代码如下

//求解相邻主索节点间的距离
#include <bits/stdc++.h>
#define sqr(x) ((x)*(x))
using namespace std;
const int maxn = 2e4 + 10;
struct node {
    double x, y, z;
} a[maxn];
int read() {
    int x = 0, f = 1;
    char ch = getchar();

    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;

        ch = getchar();
    }

    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }

    return x * f;
}
int main() {
    freopen("data.in", "r", stdin);
    int n = 2226;
    string s;
    double x, y, z;
    int tot = 0;

    //for(int i=0;i<6;i++)a[i][0]=(node){0,0,0};
    for (int i = 1; i <= n; i++) {
        cin >> s;
        scanf("%lf%lf%lf", &x, &y, &z);
        a[++tot] = (node) {
            x, y, z
        };
    }

    double sum = 0;
    int num = 0;

    for (int i = 2; i <= tot; i++) {
        double dx = a[i].x - a[i - 1].x;
        double dy = a[i].y - a[i - 1].y;
        double dz = a[i].z - a[i - 1].z;
        double ret = sqrt(sqr(dx) + sqr(dy) + sqr(dz));
        sum += ret;
        num++;
    }

    cout << sum / num << endl;
    return 0;
}

而如果你看到这个地方并不能明白链表到底是个什么东西的话,那就手推一遍吧:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值