UVA-11988 Broken Keyboard (a.k.a. Beiju Text) (链表 或 递归)

题目大意:将一个字符串改变顺序后输出。遇到“[”就将后面内容拿到最前面输出,遇到“]”就将后面的内容拿到最后面输出。

题目分析:用nxt[i]数组表示i后面的字符的下标,实际上就是以字符i为头建立链表,写法类似链式前向星。

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;

char p[100005];
int nxt[100005],head,rear;

int main()
{
    while(scanf("%s",p+1)!=EOF)
    {
        int l=strlen(p+1);
        head=rear=0;
        nxt[0]=0;
        for(int i=1;i<=l;++i){
            if(p[i]==‘[‘)
                head=0;
            else if(p[i]==‘]‘)
                head=rear;
            else{
                nxt[i]=nxt[head];
                nxt[head]=i;
                if(head==rear)
                    rear=i;
                head=i;
            }
        }
        for(int i=nxt[0];i>0;i=nxt[i])
            printf("%c",p[i]);
        puts("");
    }
    return 0;
}

  

这道题还可以用递归来解决:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;

char p[100005];

void print(int u)
{
    for(int i=u;i>=0;--i){
        if(p[i]==‘[‘){
            for(int j=i+1;j<=u;++j)
                printf("%c",p[j]);
            print(i-1);
            break;
        }else if(p[i]==‘]‘){
            print(i-1);
            for(int j=i+1;j<=u;++j)
                printf("%c",p[j]);
            break;
        }
        if(i==0){
            for(int j=0;j<=u;++j)
                printf("%c",p[j]);
        }
    }
}

int main()
{
    while(scanf("%s",p)!=EOF)
    {
        int l=strlen(p);
        print(l-1);
        printf("\n");
    }
    return 0;
}

  

时间: 2024-10-13 11:40:29

UVA-11988 Broken Keyboard (a.k.a. Beiju Text) (链表 或 递归)的相关文章

UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解

刘汝佳的题目,悲剧文本 -_-||| 这里使用vector<string>容器倒置记录数据,然后从后面输出就可以了. 难度就是不知道这样的文档到底哪里是开始输出,故此使用动态管理内存的容器比较好做. 增加了io处理的O(n)算法也没有上榜,郁闷. #include <stdio.h> #include <vector> #include <string> using std::vector; using std::string; const int MAX_

uva 11988 - Broken Keyboard (a.k.a. Beiju Text)

题意:有一个键盘坏了  会在你不知道的情况下按下home或者end  给你这个键盘的实际输入  要求输出显示器上的实际显示 解析:输入最大5MB  直接数组检索肯定会超时,用 数组模拟链表 next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边. 变量last记录显示屏最后一个字符的下标. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 const in

UVA 11988 Broken Keyboard (a.k.a. Beiju Text) STL

题目链接: UVA...... 题目描述: 给出一个字符串, 其中遇到"["是光标到最前, 遇到']'时光标又回到最后, 输出最后的文本 解题思路: 用到STL, 设置变量flag维护光标的位置, 当遇到纯字母时, 组合起来当做字符串处理, 在遇到下一个非纯字母时, 根据flag的状态一起插进deque的最前或者最后 代码: #include <iostream> #include <cstdio> #include <string> #includ

链表 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)

题目传送门 题意:训练之南P244 分析:链表模拟,维护链表的head和tail指针 #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; struct Link_list { char ch; Link_list *nex; }link_list[N]; int main(void) { while (true) { Link_list *head = link_list; Link_list *q = li

11988 - Broken Keyboard (a.k.a. Beiju Text)

Broken Keyboard (a.k.a. Beiju Text) 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 (inter

uva - Broken Keyboard (a.k.a. Beiju Text)(链表)

11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problemwith the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed(internally).Yo

Broken Keyboard (a.k.a. Beiju Text) UVA, 11988(链表)

Broken Keyboard (a.k.a. Beiju Text) From:UVA, 11988 Time Limit: 1000 MS Broken Keyboard (a.k.a. Beiju Text) 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 "hom

UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

Broken Keyboard (a.k.a. Beiju Text) 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 (inter

N - Broken Keyboard (a.k.a. Beiju Text)

N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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 som

Uva11988 Broken Keyboard (a.k.a. Beiju Text)(就是先输出括号的字符)

Description Problem B Broken Keyboard (a.k.a. Beiju Text) 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 automa