zoukankan      html  css  js  c++  java
  • hdu 1022 Train Problem I 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022

         其实是一道上数据结构课讲过的例题  只不过当时讲的是怎么用手算  一道水题又花了好长时间 其实就是栈的模拟 相当于数据结构的练习题 利用栈的先进后出的顺序 先将数进栈 等到合适的时候不停地出栈 在进栈 最后看看栈是否为空 如果能全部出栈栈为空 输出路径 否则无法实现  记录路径 我用了一个队列 其实可以使用数组 不过想到先进先出首先想到了队列queue 使用各种数据结构的时候一定要注意多次使用时要清空结构 还有就是要考虑空的情况  STL 有点使人变懒了 希望以后自己能少用吧

    粘代码:

     1 #include<iostream>
    2 #include<stack>
    3 #include<string>
    4 #include<string>
    5 #include<queue>
    6 #define in 1
    7 #define out 0
    8 using namespace std;
    9 int main()
    10 { queue<bool> road;
    11 stack<char> ans;
    12 string begin,end;
    13 int num,i,j;
    14 while(cin>>num>>begin>>end)
    15 {
    16
    17 while(!ans.empty())
    18 ans.pop();
    19 i=0;
    20 j=0;
    21 /* cout<<num<<endl;
    22 cout<<begin<<endl;
    23 cout<<end<<endl; */
    24 for(i=0;i<num;i++)
    25 {
    26 if(j>=num)
    27 break;
    28 ans.push(begin[i]);
    29 road.push(in);
    30 while(!ans.empty()&&ans.top()==end[j])// 最开始的时候没考虑ans是空的情况看看测试交了 结果数组越界
    31 {
    32 ans.pop();
    33 road.push(out);
    34 j++;
    35 if(j>=num)
    36 {break;}
    37 }
    38 }
    39 if(ans.empty())
    40 {
    41 cout<<"Yes."<<endl;
    42 while(!road.empty())
    43 { if(road.front()==1)
    44 cout<<"in"<<endl;
    45 else
    46 cout<<"out"<<endl;
    47 road.pop();
    48 }
    49 cout<<"FINISH"<<endl;
    50 }
    51 else
    52 {
    53 cout<<"No."<<endl;
    54 cout<<"FINISH"<<endl;//这最悲剧了 没仔细看题 no的后面没输出finish WA了
    55 while(!road.empty())//清空路径
    56 road.pop();
    57 while(!ans.empty())
    58 ans.pop();
    59 }
    60 }
    61 // system("pause");
    62 return 0;
    63 }


  • 相关阅读:
    Web开发快速上手
    前端概述
    Python语言进阶
    图像和办公文档处理
    网络编程
    进程和线程
    正则表达式
    面向对象进阶
    面向对象
    js 获取指定时间上月26 ,
  • 原文地址:https://www.cnblogs.com/yujiaao/p/2228696.html
Copyright © 2011-2022 走看看