• 12096 - The SetStack Computer (UVA)


    题目链接如下:

    Online Judge

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

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

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

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. // #define debug
    7. int T, N, a, b;
    8. std::mapint>, int> mp;
    9. char op[10];
    10. std::stack<int> s;
    11. std::set<int> empty;
    12. std::vectorint>> vec;
    13. void _push(std::set<int> st){
    14. if(!mp.count(st)){
    15. vec.push_back(st);
    16. mp[st] = vec.size() - 1;
    17. }
    18. s.push(mp[st]);
    19. }
    20. int main(){
    21. #ifdef debug
    22. freopen("0.txt", "r", stdin);
    23. freopen("1.txt", "w", stdout);
    24. #endif
    25. scanf("%d", &T);
    26. while(T--){
    27. scanf("%d", &N);
    28. vec.clear();
    29. mp.clear();
    30. while(N--){
    31. scanf("%s", op);
    32. if(op[0] == 'P'){
    33. _push(empty);
    34. } else{
    35. a = s.top();
    36. s.pop();
    37. if(op[0] == 'D'){
    38. s.push(a);
    39. s.push(a);
    40. } else{
    41. b = s.top();
    42. s.pop();
    43. std::set<int> tmp;
    44. if(op[0] == 'A'){
    45. tmp = vec[b];
    46. tmp.insert(a);
    47. } else{
    48. if(op[0] == 'U'){
    49. tmp = vec[a];
    50. for(auto it = vec[b].begin(); it != vec[b].end(); ++it){
    51. tmp.insert(*it);
    52. }
    53. } else if(op[0] == 'I'){
    54. for(auto it = vec[a].begin(); it != vec[a].end(); ++it){
    55. if(vec[b].find(*it) != vec[b].end()){
    56. tmp.insert(*it);
    57. }
    58. }
    59. }
    60. }
    61. _push(tmp);
    62. }
    63. }
    64. printf("%d\n", vec[s.top()].size());
    65. }
    66. printf("***\n");
    67. }
    68. #ifdef debug
    69. fclose(stdin);
    70. fclose(stdout);
    71. #endif
    72. return 0;
    73. }

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

    1. #include
    2. #include
    3. #include
    4. #include
    5. // #define debug
    6. int T, N;
    7. char op[10];
    8. std::stack> s, empStack;
    9. std::set a, b, empty;
    10. std::string toString(std::set st){
    11. std::string str = "{";
    12. for(auto it = st.begin(); it != st.end(); ++it){
    13. str += (it == st.begin() ? "" : ",");
    14. str += *it;
    15. }
    16. return str + "}";
    17. }
    18. int main(){
    19. #ifdef debug
    20. freopen("0.txt", "r", stdin);
    21. freopen("1.txt", "w", stdout);
    22. #endif
    23. scanf("%d", &T);
    24. while(T--){
    25. scanf("%d", &N);
    26. s.swap(empStack);
    27. while(N--){
    28. scanf("%s", op);
    29. if(op[0] == 'P'){
    30. s.push(empty);
    31. } else{
    32. a = s.top();
    33. s.pop();
    34. if(op[0] == 'D'){
    35. s.push(a);
    36. s.push(a);
    37. } else{
    38. b = s.top();
    39. s.pop();
    40. if(op[0] == 'A'){
    41. b.insert(toString(a));
    42. s.push(b);
    43. } else{
    44. if(op[0] == 'U'){
    45. for(auto it = a.begin(); it != a.end(); ++it){
    46. b.insert(*it);
    47. }
    48. s.push(b);
    49. } else if(op[0] == 'I'){
    50. std::set intersect;
    51. for(auto it = a.begin(); it != a.end(); ++it){
    52. if(b.find(*it) != b.end()){
    53. intersect.insert(*it);
    54. }
    55. }
    56. s.push(intersect);
    57. }
    58. }
    59. }
    60. }
    61. printf("%d\n", s.top().size());
    62. }
    63. printf("***\n");
    64. }
    65. #ifdef debug
    66. fclose(stdin);
    67. fclose(stdout);
    68. #endif
    69. return 0;
    70. }

  • 相关阅读:
    C++:STL之Vector实现
    0xC004F069错误的解决方案
    2022深入学习C++教程
    visio导出SVG矢量图在wps中显示问题
    QT下,如何获取控制台输入
    Node.js数电票、全电票查验接口示例、发票查验、票据OCR API
    ROS | ros::NodeHandle
    【蓝桥每日一题]-前缀和与差分(保姆级教程 篇1)
    ntfs是什么硬盘?ntfs硬盘如何在苹果电脑使用
    【中移芯昇】5. spi接口测试tf卡
  • 原文地址:https://blog.csdn.net/linh2006/article/details/133826912