12096 - The SetStack Computer (UVA)

题目链接如下:

Online Judge

这道题我一开始的思路大方向其实是对的,但细节怎么实现set到int的哈希没能想清楚(没想到这都能用map)。用set<string>的做法来做,测试数据小的话答案是对的,但大数据时间超时。

其实就是把所有set一一映射到int, 所以stack里每个元素就是int.

按照刘汝佳思路写的代码如下(按理每个case里stack应该先清空,但因为题目保证了没有无效操作+只需要最上面set的元素个数,不清空也没问题):

cpp 复制代码
#include <cstdio>
#include <set>
#include <stack>
#include <map>
#include <vector>
// #define debug

int T, N, a, b;
std::map<std::set<int>, int> mp;
char op[10];
std::stack<int> s;
std::set<int> empty;
std::vector<std::set<int>> vec;

void _push(std::set<int> st){
    if(!mp.count(st)){
        vec.push_back(st);
        mp[st] = vec.size() - 1;
    }
    s.push(mp[st]);
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &T);
    while(T--){
        scanf("%d", &N);
        vec.clear();
        mp.clear();
        while(N--){
            scanf("%s", op);
            if(op[0] == 'P'){
                _push(empty);
            } else{
                a = s.top();
                s.pop();
                if(op[0] == 'D'){
                    s.push(a);
                    s.push(a);
                } else{
                    b = s.top();
                    s.pop();
                    std::set<int> tmp;
                    if(op[0] == 'A'){
                        tmp = vec[b];
                        tmp.insert(a);
                    } else{
                        if(op[0] == 'U'){
                            tmp = vec[a];
                            for(auto it = vec[b].begin(); it != vec[b].end(); ++it){
                                tmp.insert(*it);
                            }
                        } else if(op[0] == 'I'){
                            for(auto it = vec[a].begin(); it != vec[a].end(); ++it){
                                if(vec[b].find(*it) != vec[b].end()){
                                    tmp.insert(*it);
                                }
                            }
                        }
                    }
                    _push(tmp);
                }
            }
            printf("%d\n", vec[s.top()].size());
        }
        printf("***\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

原先的代码如下(超时):

cpp 复制代码
#include <cstdio>
#include <set>
#include <stack>
#include <string>
// #define debug

int T, N;
char op[10];
std::stack<std::set<std::string>> s, empStack;
std::set<std::string> a, b, empty;

std::string toString(std::set<std::string> st){
    std::string str = "{";
    for(auto it = st.begin(); it != st.end(); ++it){
        str += (it == st.begin() ? "" : ",");
        str += *it;
    }
    return str + "}";
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &T);
    while(T--){
        scanf("%d", &N);
        s.swap(empStack);
        while(N--){
            scanf("%s", op);
            if(op[0] == 'P'){
                s.push(empty);
            } else{
                a = s.top();
                s.pop();
                if(op[0] == 'D'){
                    s.push(a);
                    s.push(a);
                } else{
                    b = s.top();
                    s.pop();
                    if(op[0] == 'A'){
                        b.insert(toString(a));
                        s.push(b);
                    } else{
                        if(op[0] == 'U'){
                            for(auto it = a.begin(); it != a.end(); ++it){
                                b.insert(*it);
                            }
                            s.push(b);
                        } else if(op[0] == 'I'){
                            std::set<std::string> intersect;
                            for(auto it = a.begin(); it != a.end(); ++it){
                                if(b.find(*it) != b.end()){
                                    intersect.insert(*it);
                                }
                            }
                            s.push(intersect);
                        }
                    }
                }
            }
            printf("%d\n", s.top().size());
        }
        printf("***\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
闻缺陷则喜何志丹4 分钟前
【剪枝 贪心 回溯】B4093 [CSP-X2021 山东] 发送快递|普及+
c++·算法·剪枝·贪心·洛谷
Maple_land1 小时前
Linux进程第八讲——进程状态全景解析(二):从阻塞到消亡的完整生命周期
linux·运维·服务器·c++·centos
ajassi20001 小时前
开源 C++ QT QML 开发(十一)通讯--TCP服务器端
c++·qt·开源
lyp90h1 小时前
高效SQLite操作:基于C++模板元编程的自动化封装
c++
minji...2 小时前
Linux相关工具vim/gcc/g++/gdb/cgdb的使用详解
linux·运维·服务器·c++·git·自动化·vim
_OP_CHEN2 小时前
C++基础:(九)string类的使用与模拟实现
开发语言·c++·stl·string·string类·c++容器·stl模拟实现
爱编程的化学家2 小时前
代码随想录算法训练营第27天 -- 动态规划1 || 509.斐波那契数列 / 70.爬楼梯 / 746.使用最小花费爬楼梯
数据结构·c++·算法·leetcode·动态规划·代码随想录
数字化顾问3 小时前
C++分布式语音识别服务实践——架构设计与关键技术
c++
智能化咨询3 小时前
C++分布式语音识别服务实践——性能优化与实战部署
c++
ajassi20004 小时前
开源 C++ QT QML 开发(十四)进程用途
c++·qt·开源