zoukankan      html  css  js  c++  java
  • 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;
    }
    View Code

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

  • 相关阅读:
    Scala篇:Scala环境及IDEA配置
    大数据篇:Hive
    大数据篇:Flume
    大数据篇:Kafka
    15.百万考生成绩如何排序
    Tomcat 架构原理解析到架构设计借鉴
    服务设计思考:平台化
    Druid:通过 Kafka 加载流数据
    12.分而治之-归并排序
    11.经典O(n²)比较型排序算法
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7241587.html
Copyright © 2011-2022 走看看