zoukankan      html  css  js  c++  java
  • 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用法。。

  • 相关阅读:
    用Java实现一个简单的DBMS(总结)
    Android小记(整理一下自己犯过的错误)
    在华为云上开启FTP服务并建立FTP站点来从本地向服务器发送和下载文件
    AS中使用真机调试时出现解析错误的问题
    AS中加载gradle时出现Gradle sync failed: Could not find com.android.tools.build:gradle.的错误
    解决AS加载gradle时出现的Could not find com.android.tools.build:gradle:3.5.0.的错误
    过滤器
    View的呈现
    Asp.net MVC Action同步异步执行
    Model验证
  • 原文地址:https://www.cnblogs.com/riskyer/p/3237119.html
Copyright © 2011-2022 走看看