首页 > 其他分享 >uva12096集合栈计算机 The SetStack Computer

uva12096集合栈计算机 The SetStack Computer

时间:2023-12-05 10:22:53浏览次数:34  
标签:set ID SetStack Computer uva12096 x2 idsearch Set Stack

洛谷链接集合栈计算机 The SetStack Computer - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

一道典型的以栈为背景的数据结构题。题目简单但是程序却并不简单(个人观点)。普及组的难度有点低了感觉。

个人认为这道题目可以帮助自己熟悉或者说更好的掌握STL的使用以及妙用。

难点:1、首先出栈入栈的是集合,如果单纯的使用set的话会出现set<set<set<...>>>的情况。所以用一个ID 代替一种集合的想法就显而易见了(很值得学习)。

2、并集交集的实现。两种方法,一种是自带的函数set_union(取并集)、set_intersection(取交集)、set_difference(取差集)。

有兴趣可以了解关于C++中使用set_union、set_intersection、set_difference,set_symmetric_difference、merge的总结-CSDN博客,其中也展现了如果不用自带函数该咋做。

#include<bits/stdc++.h>
using namespace std;
typedef set<int> Set;  //类型定义使代码更简洁 
map<Set,int> ID;   //由map映射来表示一个集合的ID 
vector<Set> idsearch;  //由idsearch的右边界来表示ID,其次还可以根据idsearch[i]可以取出对应的Set 
int IDsearch(Set x){ //查找ID 
    if (ID.count(x)) return ID[x];
    idsearch.push_back(x);
    return ID[x]=idsearch.size()-1;
}
int main(){
    stack<int> Stack; //栈 
    int t;
    cin>>t;
    while(t--){
        int n;string s;
        cin>>n;
        for (int i=0;i<n;i++){
            cin>>s;
            if (s[0]=='P') Stack.push(IDsearch(Set()));
            else if (s[0]=='D') Stack.push(Stack.top());
            else {
                Set x1=idsearch[Stack.top()];Stack.pop();
                Set x2=idsearch[Stack.top()];Stack.pop();
                Set x;
                if (s[0]=='U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));  //取并集 
                if (s[0]=='I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));//取交集 
                if (s[0]=='A') {x=x2;x.insert(IDsearch(x1));}
                Stack.push(IDsearch(x));
            }
            cout<<idsearch[Stack.top()].size()<<endl;
        }
        cout<<"***"<<endl;
    }
    return 0;
}

Ps:个人认为是一道很有价值的题目

标签:set,ID,SetStack,Computer,uva12096,x2,idsearch,Set,Stack
From: https://www.cnblogs.com/purple123/p/17876619.html

相关文章

  • Improving Computer Vision Accuracy using Convolutions
    ImprovingComputerVisionAccuracyusingConvolutions‍在前面的课程中,你们了解了如何使用包含三层的深度神经网络(DNN)进行时装识别,这三层分别是输入层(数据的形状)、输出层(所需输出的形状)和隐藏层。你试验了不同大小的隐藏层、训练epochs数等对最终准确率的影响。为方便起见,这......
  • Beyond Hello World, A Computer Vision Example
    BeyondHelloWorld,AComputerVisionExampledlaicourse/Course1-Part4-Lesson2-Notebook.ipynbatmaster·lmoroney/dlaicourse(github.com)StartCoding导入TensorFlowimporttensorflowastfprint(tf.__version__)从tf.keras数据集API中获取时尚......
  • Computer vision: models, learning and inference
    http://www.computervisionmodels.com/13.2.3SIFTdetectorSIFT尺度不变特征转换sasecondmethodforidentifyinginterestpoints一个尺度和对应兴趣点定位141516......
  • ISOM 3029 - Computer Programming Using C++
    以下是C++程序的问题。作业的硬拷贝和软拷贝都应按时提交。全部的程序(.cpp文件)将被压缩并上传到“提交作业1”按钮。压缩文件应与您的学生一起命名编号,例如“ba12345_Ass1.zip”。问题1:彩票计划(50%)编写一个C++程序,在开始时输出一条问候信息,然后生成6个肯定信息对于彩票游......
  • 无涯教程-批处理 - NET COMPUTER函数
    添加或删除连接到Windows域控制器的计算机。NETCOMPUTER-语法NETCOMPUTER\\computername{/ADD|/DEL}NETCOMPUTER-示例NETCOMPUTER\\dxbtest/ADD上面的命令会将名称为dxbtest的计算机添加到Windows域控制器所在的域中。参考链接https://www.learnfk.com/batch-......
  • Design of A Basic Computer Model With Stack Function
    IntroductionLastweekendIundertookaprojecttodesignabasiccomputermodelfromclockgeneratordesigntomicroinstructionencoding.AndIpreferablychosetodesignabasicmodelwithcommonstackfunctions.MyReportContributorsZhihao......
  • CF638D Three-dimensional Turtle Super Computer
    什么大力爆搜题不妨考虑枚举要拿掉的位置,考虑怎么检验它是某两个点之间必经之点简单手玩一下会发现如果存在这么一条路径,那么我们一定可以把该路径的端点定为与要拿掉的点距离为\(1\)的点上(即与要拿掉的点上下左右前后\(6\)连通)因此我们把这些点找出来后爆枚点对,判断路径是否唯......
  • 洛谷355BAJ-Bytecomputer8
    这一道题如果直接做是没有什么思路的,所以我们合理猜测应该是有什么结论看这个数列最开始就只有三个值,所以我们猜测最后也只有这三个值下面是证明首先第一个数最小是-1,所以所有数的下界是-1其次如果存在某一个数大于1,我们找到这个数列最前面的这个数,那他前面的数肯定是1,然后对......
  • CS61A: Structure and Interpretation of Computer Programs 笔记
    FunctionsEnvironmentDiagrams:左侧为Frames,右侧为Objects。Name类似变量名,它们存储在Frame中,指向各种各样的Objects,比如值或函数。一个Name同时只能指向一个Object,但可以改变自身指向,不受“类型”影响(Name根本没有固定的“类型”概念)。Assignment的过程是计算'='......
  • ODPS/MaxComputer 自定义UDF、UDTF函数
    ODPS/MaxComputer自定义UDF、UDTF函数前置条件:创建Maven工程导入jar包和打包工具:<dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-sdk-udf</artifactId><version>0.29.10-public</ver......