F - ACboy needs your help again! (模拟)
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
InputThe input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.OutputFor each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.Sample Input
- 4
- 4 FIFO
- IN 1
- IN 2
- OUT
- OUT
- 4 FILO
- IN 1
- IN 2
- OUT
- OUT
- 5 FIFO
- IN 1
- IN 2
- OUT
- OUT
- OUT
- 5 FILO
- IN 1
- IN 2
- OUT
- IN 3
- OUT
Sample Output
- 1
- 2
- 2
- 1
- 1
- 2
- None
- 2
- 3
题意讲的很清楚,就是用栈和队列来模拟,但是提交的时候WA了两次,后来才发现当进入过一组数据后,要把栈和队列清空
AC代码
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<stack>
- #include<queue>
- const int maxn = ;
- using namespace std;
- int main()
- {
- stack<int> sT;
- queue<int> que;
- int t, num;
- char xx[];
- char s1[] = "IN", s2[] = "OUT";
- char s3[] = "FIFO", s4[] = "FILO";
- scanf("%d", &t);
- while(t--)
- {
- int n;
- char str[];
- scanf("%d %s", &n, &str);
- if(strcmp(str, s3) == )
- {
- for(int i = ; i < n; i++)
- {
- scanf("%s", &xx);
- if(strcmp(xx, s1) == )
- {
- scanf("%d", &num);
- que.push(num);
- }
- if(strcmp(xx, s2) == )
- {
- if(que.empty())
- {
- printf("None\n");
- }
- else
- {
- printf("%d\n", que.front());
- que.pop();
- }
- }
- }
- while(!que.empty())
- que.pop();
- }
- if(strcmp(str, s4) == )
- {
- for(int i = ; i < n; i++)
- {
- scanf("%s", &xx);
- if(strcmp(xx, s1) == )
- {
- scanf("%d", &num);
- sT.push(num);
- }
- if(strcmp(xx, s2) == )
- {
- if(sT.empty())
- {
- printf("None\n");
- }
- else
- {
- printf("%d\n", sT.top());
- sT.pop();
- }
- }
- }
- while(!sT.empty())
- sT.pop();
- }
- }
- return ;
- }
在网上找了一些其他人的代码,发现也有人用vector来解题,也要学习一下
- /*
- 题意:这个东西总共两种模式:栈和队列,然后模拟
- 解体思路:既然是模拟,就没什么搞得了。开搞
- */
- #include<bits/stdc++.h>
- using namespace std;
- int t,n;
- int a;
- string op,name;
- vector<int>v;
- void init(){
- v.clear();
- }
- int main(){
- //freopen("in.txt","r",stdin);
- scanf("%d",&t);
- while(t--){
- scanf("%d",&n);
- cin>>name;
- init();
- if(name=="FIFO"){//先进先出
- for(int i=;i<n;i++){
- cin>>op;
- if(op=="IN"){//进入
- scanf("%d",&a);
- v.push_back(a);
- }else{//出
- if(v.size()==){
- printf("None\n");
- }else{
- printf("%d\n",v[]);
- v.erase(v.begin());
- }
- }
- }
- }else{//先进后出
- for(int i=;i<n;i++){
- cin>>op;
- if(op=="IN"){//进入
- scanf("%d",&a);
- v.push_back(a);
- }else{//出
- if(v.size()==){
- printf("None\n");
- }else{
- printf("%d\n",v[v.size()-]);
- v.erase(v.end()-);
- }
- }
- }
- }
- }
- return ;
- }
F - ACboy needs your help again! (模拟)的更多相关文章
- Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Vjudge - F - 比前面更简单的模拟
2017-07-16 07:31:35 writer:pprp 题目介绍:很基础的string用法 题目如下: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字 ...
- 浅谈Javascript事件模拟
事件是用来描述网页中某一特定有趣时刻的,众所周知事件通常是在由用户和浏览器进行交互时触发,其实不然,通过Javascript可以在任何时间触发特定的事件,并且这些事件与浏览器创建的事件是相同的.这就意 ...
- python 反人类函数式编程模拟while和if控制流
比如下面这个简单明了的命令式程序,它不断捕捉用户输入的内容,然后对其求和.直到用户输入一个以'0'开头的字符串,停止捕捉. while 1: line = input() ': print(sum(m ...
- 模拟登陆,selenium,线程池
一 . 模拟登陆案例(识别验证码) 1 . 打码平台 - 云打码 : www.yundama.com 使用步骤 : - 注册两个账户,普通用户和开发者用户 : - 登陆 普通用户查看余额 登陆开发 ...
- 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)
A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...
- awk 常用选项及数组的用法和模拟生产环境数据统计
awk 常用选项总结 在 awk 中使用外部的环境变量 (-v) awk -v num2="$num1" -v var1="$var" 'BEGIN{print ...
- H-Updating a Dictionary (模拟)
In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...
- C#接口显示实现在实际开发中的作用
摘要 任何一个C#入门的程序员都知道——当一个类型在实现接口的时候,有两种方法实现:显式实现.隐式实现.而且大家也都知道,当一个类型实现的两个接口存在相同成员定义时,显示实现可以解决这种情况. 但是, ...
随机推荐
- Python时间日期函数讲解
所有日期.时间的api都在datetime模块内. 1. 日期输出格式化 datetime => string import datetime now = datetime.datetime.n ...
- AE插件之SKYBOX CONVERTER
AE插件之SKYBOX CONVERTER AE插件SKYBOX CONVERTER的主要作用是开发全景视频或者制作全景图片时,对添加的字幕.图片进行扭曲. 下载目录:http://www.gfxca ...
- MYCAT实战之分片迁移
实践扩容 1.要求: travelrecord 表定义为10个分片,尝试将10个分片中的 2 个分片转移到第二台MySQL上, 并完成记录要求,最快的数据迁移做法,中断业务时间最短 2.针对分片以及迁 ...
- scala中存在的问题
2017-12-27 scala学习中存在的问题: 1.表达式的概念要搞清楚 2.八种基本数据类型要搞清楚 Byte\Short\Int\Long\Char\Boolean\Double\Float都 ...
- 使用jq.lazyload.js,解决设置loading图片的问题
最近在使用lazyload的时候,遇上一个问题.当对img做宽100%时,就是placeholder的loading图片也会100%宽,这样一般来说loading图片就会变得很大.实在是不能应用到项目 ...
- Objects & Class
[Objects & Class] 1.定义一个类. 上述代码中,numberOfSides是实例变量,simpleDescription也是实例方法. 2.创建实例,使用实例. 3.init ...
- java Web jsp和servlet的关系
JSP在本质上就是SERVLET,但是两者的创建方式不一样Servlet完全是JAVA程序代码构成,擅长于流程控制和事务处理,通过Servlet来生成动态网页很不直观JSP由HTML代码和JSP标签构 ...
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
通过 正则表达式 来获取一个网页中的所有的 URL链接,并下载这些 URL链接 的源代码 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 ...
- c语言实践输出某个区间中不是3的倍数的偶数
OK,先审题,我们最后要输出的那些数是需要满足两个条件的,第一个条件是,这个数不是3的倍数,第二个条件是这个数是偶数.也就是这样的数需要同时满足这两个条件的时候才把这个数输出. 不是3的倍数这个条件在 ...
- javascript DES加密
研究联通wifi登陆中,发现了一个名为"encryption.js"的文件.这个文件一看即知是加密过的,首先自己尝试去手工解密,看到太烦琐了,忽然想到网上有js解密工具,遂决定用来 ...