1596 - Bug Hunt (UVA)

题目链接如下:

https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=448&page=show_problem&problem=4471

我的代码如下:

cpp 复制代码
#include <iostream>
#include <string>
#include <map>
// #define debug

struct arr{
    int len;
    std::map<int, int> mp;
};
std::string s;
int bug;
std::map<std::string, arr> arrMp;

bool validJudge(std::string &str, int &value){
    if (str.find("[") == std::string::npos){
        value = std::stoi(str);
        return true;
    }
    int left = str.find("[");
    int right = str.rfind("]");
    int v;
    std::string temp1, temp2;
    temp1 = str.substr(left + 1, right - left - 1);
    temp2 = str.substr(0, left);
    if (!validJudge(temp1, v) || arrMp[temp2].len <= v || arrMp[temp2].mp.find(v) == arrMp[temp2].mp.end()){
        return false;
    }
    value = arrMp[temp2].mp[v];
    return true;
}

bool judge(std::string &str){
    int left, right, v1, v2;
    std::string temp, temp1, temp2;
    if (str.find("=") == std::string::npos){
        left = str.find("[");
        right = str.rfind("]");
        temp = str.substr(left + 1, right - left - 1);
        if (!validJudge(temp, v1)){
            return false;
        }
        arrMp[str.substr(0, left)].len = v1;
        return true;
    }
    int equLoc = str.find("=");
    left = str.find("[");
    right = str.rfind("]", equLoc);
    temp1 = str.substr(left + 1, right - left - 1);
    temp2 = str.substr(equLoc + 1);
    if (!validJudge(temp1, v1) || v1 >= arrMp[str.substr(0, left)].len || !validJudge(temp2, v2)){
        return false;
    }
    temp = str.substr(0, left);
    arrMp[temp].mp[v1] = v2;
    return true;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, s) && s != "."){
        bug = 0;
        int i = 0;
        arrMp.clear();
        do {
            if (bug) continue;
            ++i;
            if (!judge(s)){
                bug = i;
            }
        } while (getline(std::cin, s) && s != ".");
        printf("%d\n", bug);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
在路上看风景7 小时前
19. 成员初始化列表和初始化对象
c++
zmzb01037 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹8 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
孞㐑¥9 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
MZ_ZXD00110 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
A星空12311 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
凡人叶枫12 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
会叫的恐龙12 小时前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串
小糯米60112 小时前
C++顺序表和vector
开发语言·c++·算法
独望漫天星辰12 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++