• UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】


     先看UVA11995

    两份代码一份直接用C写的,一份用STL写的闭嘴

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    #include <stack>
    //#include <priority_queue>
    using namespace std;
    int a[1005];
    int b[1005];
    int c[1005];
    struct ope
    {
      int x;
      int y;
    }op[1005];
    int v[4];//1队列 2栈 3优先队列
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            memset(c,0,sizeof(c));
            memset(v,0,sizeof(v));
            int cnt1=0,cnt2=0,cnt3=0;
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&op[i].x,&op[i].y);
            }
            for(int i=0;i<n;i++)
            {
                if(op[i].x==1)
                {
                    a[cnt1++]=op[i].y;
                }
                if(op[i].x==2)
                {
                    if(cnt1-1<0) { v[1]=1;}
                    else
                    {
                        int temp=0;
                        for(int jj=0;jj<cnt1;jj++)
                        {
                            if(a[jj]==0){temp++; continue ;}
                            else
                            {
                                if(a[jj]==op[i].y)
                                  {
                                      a[jj]=0;
                                      break;
                                  }
                                else {v[1]=1;break;}
                            }
                        }
                        if(temp==cnt1) v[1]=1;
                    }
                }
                if(op[i].x==1)
                {
                    b[cnt2++]=op[i].y;
                }
                if(op[i].x==2)
                {
                    int temp=0;
                    if(cnt2-1<0) {v[2]=1;}
                    else
                    {
                        for(int jj=cnt2-1;jj>=0;jj--)
                        {
                            if(b[jj]==0) {temp++;continue;}
                            else
                            {
                               if(b[jj]==op[i].y)
                                {b[jj]=0;break;}
                               else v[2]=1;break;
    
                            }
                        }
                       if(temp==cnt2) {v[2]=1;}
    
                    }
                }
                if(op[i].x==1)
                {
                    c[cnt3++]=op[i].y;
                }
                if(op[i].x==2)
                {
                    if(cnt3-1<0) v[3]=1;
                    else
                    {
                        int temp3,max=-1;
                        for(int jj=0;jj<cnt3;jj++)
                            if(c[jj]>max){temp3=jj; max=c[jj];}
                        if(max==op[i].y)  c[temp3]=0;
                        else v[3]=1;
                    }
                }
    
            }
            if(!v[1]&&v[2]==1&&v[3]==1)
               printf("queue
    ");
            else if(v[1]==1&&!v[2]&&v[3]==1)
               printf("stack
    ");
            else if(v[1]==1&&v[2]==1&&!v[3])
               printf("priority queue
    ");
            else if(v[1]==1&&v[2]==1&&v[3]==1)
               printf("impossible
    ");
            else printf("not sure
    ");
        }
        return 0;
    }
    
    
    
    
    
    
    
    
    STL版~
    
    #include<cstdio>
    #include<stack>
    #include<queue>
    using namespace std;
    const int maxn = 1000+100;
    int id[maxn],x[maxn],n;
    bool isStack(){
        stack<int> s;
        for(int i=0;i<n;i++){
            if(id[i]==1) s.push(x[i]);
            else{
                if(s.empty())  return false;
                int val=s.top();  s.pop();
                if(x[i]!=val) return false;
            }
        }
        return true;
    }
    bool isQueue(){
         queue<int >q;
          for(int i=0;i<n;i++){
            if(id[i]==1) q.push(x[i]);
            else{
                if(q.empty()) return false;
                int val=q.front();  q.pop();
                if(x[i]!=val) return false;
            }
        }
        return true;
    
    }
    bool isPriority(){
        priority_queue<int > q;
          for(int i=0;i<n;i++){
            if(id[i]==1) q.push(x[i]);
            else{
                 if(q.empty()) return false;
                int val=q.top();  q.pop();
                if(x[i]!=val) return false;
            }
        }
        return true;
    }
    int main(){
        while(scanf("%d",&n)!=EOF){
            bool st=false,qu=false,pr=false;
            for(int i=0;i<n;i++){
                scanf("%d %d",&id[i],&x[i]);
            }
            st=isStack(); qu=isQueue();  pr=isPriority();
            if(!st&&!qu&&!pr)  puts("impossible");
            else if((!st&&qu&&pr)||(!qu&&st&&pr)||(!pr&&qu&&st)||pr&&qu&&st){
                puts("not sure");
            }
            else if(st) puts("stack");
            else if(qu) puts("queue");
            else if(pr) puts("priority queue");
    
        }
        return 0;
    }
    

    queue,stack,priority_queue取顶部or底部元素,front,top,back,push,pop.......
     

    接下来是UVA11991

    附代码

    #include<iostream>
    #include<vector>
    #include<map>
    #include<stdio.h>
    using namespace std;
    map<int,vector<int> > a;
    int main()
    {
        int n,m,x,y;
        while(scanf("%d%d",&n,&m)==2)
        {
            a.clear();
            for(int i=0;i<n;i++)
            {
                scanf("%d",&x);
                if(!a.count(x))
                   a[x]=vector<int>();
                a[x].push_back(i+1);
            }
            while(m--)
            {
                scanf("%d%d",&x,&y);
                if(!a.count(y)||a[y].size()<x)
                   printf("0
    ");
                else printf("%d
    ",a[y][x-1]);
            }
        }
        return 0;
    }
    


    map,vector用法。。

    map里面的count用法。。

  • 相关阅读:
    JS实现继承的几种方式
    跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析
    cordova生成的android项目导入到Android studio 2.X 中遇到的问题解决方案
    链操作相关命令(包括启动,重启,删除)
    冷钱包和热钱包有什么区别?
    常用命令之git/linux
    centos安装git,go,shasum,okexchain环境
    iterm2的下载安装与配置
    使用jsdoc-to-markdown提前js文件的文档
    基于sphinx的文档(一)将md转为rst
  • 原文地址:https://www.cnblogs.com/riskyer/p/3237119.html
Copyright © 2020-2023  润新知