zoukankan      html  css  js  c++  java
  • DS实验题 word

    题目:


    代码:

    //
    //  main.cpp
    //  word
    //
    //  Created by wasdns on 16/11/11.
    //  Copyright © 2016年 wasdns. All rights reserved.
    //
    
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <cstdio>
    #include <stack>
    #include <cstdlib>
    using namespace std;
    
    string store[10005];                        //存储输出的字符串
    
    /*
     输出函数PrintStack:
        先将栈中元素存到store数组中,再逆序输出数组中元素。
     */
    
    void PrintStack(stack<string> ins)
    {
        int cnt = 1;
        
        while (!ins.empty()) {
            
            store[cnt++] = ins.top();
            
            ins.pop();
        }
        
        for (int i = cnt-1; i > 0; i--) {
            cout << store[i] << " ";
        }
        
        cout << endl;
    }
    
    int main()
    {
        int i, n;
        
        stack<string> ins, temp;                //字符串栈,ins为题目中的word栈
                                                //temp为被ctrl+z操作弹出的字符串
        cin >> n;
        
        getchar();
        
        for (i = 0; i < n; i++)
        {
            string statement;                   //输入的字符串
            
            cin >> statement;
            
            if (statement == "ctrl+z")          //当字符串为ctrl+z时
            {
                if (!ins.empty()) {             //且ins栈不为空时
                    
                    temp.push(ins.top());
                    ins.pop();                  //从ins栈中弹出一个字符串并压入temp
                    
                    continue;
                }
            }
            
            else if (statement == "ctrl+y")     //当字符串为ctrl+y时
            {
                if (!temp.empty()) {            //且temp栈不为空的时候
                    
                    ins.push(temp.top());
                    temp.pop();                 //从temp栈中弹出一个字符串并压入ins
                
                }
                
                else continue;
            }
            
            else if (statement[0] == 'i')       //当进行input操作时
            {
                string str;
                
                cin >> str;
                
                ins.push(str);                  //将字符串压入ins栈
                
                while (!temp.empty()) {         //按照题目要求,将temp栈清空
                    temp.pop();
                }
            }
            
        }
        
        //输出:
        
        if (ins.empty())
            cout << "No output" << endl;
        else
            PrintStack(ins);
        
        
        return 0;
    }
    
    

    结果:

    结论:

    这道题目,逻辑主线相对较为清晰,但是需要注意对状态的保存(需要再维护一个栈,保存delete操作弹出的字符串)。

    之前没有AC的原因也是因为忽略了上面这一点,只对最近一次弹出的字符串进行保存,一旦出现如下情况:

    6
    input a
    input b
    ctrl+z
    ctrl+z
    ctrl+y
    ctrl+y
    
    

    输出为b,但是正确的答案应该是b a

    2016/11/11

  • 相关阅读:
    owlsuddimatchmaker安装
    类集
    jsp基本语法
    心得思路记录下
    nyoj517 最小公倍数
    poj1250 Tanning Salon
    poj1686 Lazy Math Instructor
    poj3250 Bad Hair Day
    poj1047 Round and Round We Go
    poj2359 Questions
  • 原文地址:https://www.cnblogs.com/qq952693358/p/6055599.html
Copyright © 2011-2022 走看看