vector,stack,queue--《算法竞赛入门到进阶》

依旧是《算法竞赛入门到进阶》的学习哦!!

注:代码和部分知识点以及注释引用自《算法竞赛入门到进阶》(孩子水平有限还是用大家的代码比较有质量的保障)而且我只是把我想跟大家分享的知识点写出来,注意注意,题目来自vj,有一部分注释是我自己写的,水平非常有限,啊等我什么时候成大佬了就。。(一切尽在不言中)
这篇文章是我之前在b站写的现在一起放过来啦~~~

容器

(1)vector

vector是STL的动态数组。vector容器是一个模板类,能存放任何类型的对象。

vector的定义示例:

定义int型数组:

                     vector <int> a ;               默认初始化,a为空
                     vector<int>b(a);               用a定义b;
                     vector<int>a(100);             a有100个值为0的元素。
                     vector<int>a(100,6);           有100个值为6的元素

定义string型数组:

                     vector<string>a(b.begin(),b.end());     a是b的复制                          

定义结构型数组:

                    struct point{int x,y;};
                    vector<point>a;   a用来存坐标

书中例子:
圆桌上围坐着2n个人。其中n个人是好人,另外n个人是坏人。如果从第一个人开始数数,数到第m个人,则立即处死该人;然后从被处死的人之后开始数数,再将数到的第m个人处死……依此方法不断处死围坐在圆桌上的人。试问预先应如何安排这些好人与坏人的座位,能使得在处死n个人之后,圆桌上围坐的剩余的n个人全是好人。
Input
多组数据,每组数据输入:好人和坏人的人数n(<=32767)、步长m(<=32767);
Output
对于每一组数据,输出2n个大写字母,‘G’表示好人,‘B’表示坏人,50个字母为一行,不允许出现空白字符。相邻数据间留有一空行。
Sample Input
2 3
2 4
Sample Output
GBBG
BGGB

#include <bits/stdc++.h>//大家注意啊,要是在vj上,似乎通不过这个头文件,

                                         后来我都是自己写的头文件。
 using namespace std;
 int main()
 {
     vector <int> table;//模拟一个圆桌
     int n,m;
     while(cin>>n>>m)//输入两个数,n和m
     {
         table.clear();//清空 (vector的常用操作,格式就是a.clear())
         for(int i=0;i<2*n;i++)
         {
             table.push_back(i);

          //初始化(实际上在vector的常用操作中,a.push_back(i)表示尾部插入为i的值)
         }
         int pos=0;//记录当前位置
         for(int i=0;i<n;i++)
         {
             pos=(pos+m-1)%table.size();

               //圆桌是个环,取余处理(在vector的常用操作中,int size=a.size(),说明元素个数)
             table.erase(table.begin()+pos);

                        //vector的常用操作:a.erase(a.begin()+i),删除第i+1个 元素
         }
         int j=0;
         for(int i=0;i<2*n;i++)//打印预先安排座位
         {
             if(!(i%50)&&i)//50个字母为一行
             {
                 cout<<endl;
             }
             if(j<table.size()&&i==table[j])//table留下的都是好人
             {
                 j++;
                 cout<<"G";
             }
             else
             {
                 cout<<"B";
             }
         }
    cout<<endl<<endl;//留一个空行
     }
     return 0;
  } 

注:一些常用操作中例子中已经用过了,下面是未包含的:

                       a.insert(a.begin()+i,k);                  在第i个元素前面插入k
                       a.insert(a.end(),10,5);                   尾部插入10个值为5的元素
                       a.erase(a.begin()+i,a.begin()+j);         删除区间[i,j-1]的元素
                       a.resize(n);                              数组大小变为n
                       reserve(a.begin(),a.end());             用函数reserve()翻转数组
                       sort(a.begin(),a.end());                  用函数sort()排序,从小到大排

**

(2)栈和stack,队列和queue

**

翻转字符串:

输入:“olleh !dlrow” 输出“hello world!”

#include <iostream>
#include <stack>//这里就是我自己写的头文件
 using namespace std;
 int main()
 {
     int n;
     char ch;
     scanf("%d",&n);
     getchar();//https://blog.csdn.net/qq_37059136/article/details/80280991这个网址里有关于 getchar()的用法大家可以去瞅瞅~
     while(n--)
     {
         stack<char>s;//定义栈,为char型
         while(true)//while(true)就是无限循环语句。因为括号中的条件为true,所以永远不会跳出循环,除非语句块中有break语句才能跳出循环。
         {
             ch=getchar();
             if(ch==' '||ch=='\n'||ch==EOF)
             {
                 while(!s.empty() )//s.empty();检查栈是否为空,如果为空,返回true,否则返回false。这里 while()中的意思是如果栈不为空就一直执行while循环的语句
                 {
                     printf("%c",s.top());//返回栈顶的元素,但不会删除 .
                     s.pop() ;//删除栈顶的元素,但不会返回.(在出栈时需要进行两步操作,即先获得栈顶元素,再pop()删除栈顶元素)
                 }
                if(ch=='\n'||ch==EOF)
               {
                    break;
               }
                printf(" ");
            }
            else s.push(ch);//把ch放回栈顶
         }
         printf("\n");
     }
     return 0;
 }

题目:

ACboy was kidnapped!!
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!

Input

The 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.

Output

For 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

这道题是我从vj上找到的并引用过来的题目,书上并没有完整的题目
中文翻译过来的话实在是有点麻烦(好吧我就是懒得写出来),所以请大家耐心看看题目中的例子,相信一定能明白(实在不行的话还要各种翻译工具对不对吧~)

#include <iostream>
#include <queue>//队列的头文件
#include<stack>//栈的头文件 

#include<string>//字符串的头文件(这里我的编译器能过但是vj过不去还是写了吧)

 using namespace std;
 int main()
 {
     int t,n,temp;
     cin>>t;
     while(t--)
     {
         string str,str1;
         queue<int>Q;
         stack<int>S;
         cin>>n>>str;
         for(int i=0;i<n;i++)
         {
             if(str=="FIFO")//FIFO很明显是队列的特点(first in first out) 。
             {
                 cin>>str1;
                 if(str1=="IN")//这里注意一下如果是字符串用“”双引号,字符用‘’单引号。
                 {
                     cin>>temp;
                     Q.push(temp);//把temp放进队列。
                 }
                 if(str1=="OUT")
                 {
                     if(Q.empty()) cout<<"None"<<endl;//判断队列是否为空 。
                     else{
                         cout<<Q.front()<<endl;//Q.front()表示返回队首元素,但不会删除。
                         Q.pop();//删除队首元素。
                     }
                 }
             }
             else{   //这下面和上面的队列思路差不多就不写啥注释了
                 cin>>str1;
                 if(str1=="IN"){
                     cin>>temp;S.push(temp);
                 }
                 if(str1=="OUT"){
                     if(S.empty() )cout<<"None"<<endl;
                     else{
                         cout<<S.top()<<endl;
                         S.pop();
                     }
                 }
             }
         }
      }
      return 0;
  }

注:栈和队列在书上分成两个模块了,我就放一起了,因为第二个例题算是综合应用嘛。栈和队列的概念,我觉得就两句话:栈:先进后出,队列:先进先出(第二个例题简直就是帮助理解的很好代表)(仅是菜鸟的个人意见哦~~~)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值