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

  题目链接: UVA......

  题目描述: 给出一个字符串, 其中遇到"["是光标到最前, 遇到‘]‘时光标又回到最后, 输出最后的文本

  解题思路: 用到STL, 设置变量flag维护光标的位置, 当遇到纯字母时, 组合起来当做字符串处理, 在遇到下一个非纯字母时, 根据flag的状态一起插进deque的最前或者最后

  代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <iterator>
using namespace std;

deque<string> dq;

int main() {
    string str;
    while( cin >> str ) {
        dq.clear();
        string temp = "";
        int len = (int)str.length();
        int flag = 1; // 记录当前状态 0 (光标开头)1(光标末尾)
        int i = 0;

        while(1) {
            if( i >= len ) break;
            while( i < len && str[i] != ‘[‘ && str[i] != ‘]‘ ) {
                temp += str[i];
                i++;
            }

            if( flag == 0 ) {
                dq.push_front(temp);
            }
            else {
                dq.push_back(temp);
            }
            temp = "";
            if( str[i] == ‘[‘ ) {
                flag = 0;
                i++;
            }
            else {
                flag = 1;
                i++;
            }
        }
        deque<string>::iterator it;
        for( it = dq.begin(); it != dq.end(); it++ ) {
            cout << *it;
        }
        cout << endl;
    }
    return 0;
}

  思考: 这是以前自己认为很难的一道题,我记得是紫书上的, 当时想了半天也没做上来, 现在写出来了就说明自己真的进步了, 继续做题

时间: 2024-08-06 20:04:46

UVA 11988 Broken Keyboard (a.k.a. Beiju Text) STL的相关文章

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)

题目传送门 题意:训练之南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