1 条题解

  • 1
    @ 2025-6-7 20:25:34
    
    #include <iostream>
    #include <vector>
    #include <map>
    #include <sstream>
    #include <algorithm>
    using namespace std;
    
    struct Statement {
        string speaker;
        string content;
    };
    
    vector<string> split(const string &s, char delim) {
        vector<string> tokens;
        string token;
        istringstream iss(s);
        while (getline(iss, token, delim))
            if (!token.empty()) tokens.push_back(token);
        return tokens;
    }
    
    bool isGuiltyStatement(const string &content, const string &suspect) {
        if (content == "I am guilty.") return true;
        vector<string> parts = split(content, ' ');
        if (parts.size() >= 3 && parts[1] == "is" && parts[2] == "guilty.")
            return parts[0] == suspect;
        return false;
    }
    
    bool checkScenario(int M, int N, const vector<string> &names, 
                      const vector<Statement> &statements, const string &suspect) {
        map<string, bool> isLiar; // true表示说谎者
        
        // 检查每个陈述的真假
        for (const auto &stmt : statements) {
            bool truthValue = isGuiltyStatement(stmt.content, suspect);
            if (stmt.speaker == suspect) truthValue = !truthValue; // 凶手总说谎
            
            if (!truthValue) isLiar[stmt.speaker] = true;
        }
        
        // 验证说谎人数是否符合
        int liarCount = 0;
        for (const auto &name : names) 
            if (isLiar[name]) liarCount++;
        
        return liarCount == N;
    }
    
    int main() {
        int M, N, P;
        cin >> M >> N >> P;
        cin.ignore();
        
        vector<string> names(M);
        for (int i =  0; i < M; ++i)
            getline(cin, names[i]);
        
        vector<Statement> statements(P);
        for (int i = 0; i < P; ++i) {
            string line;
            getline(cin, line);
            size_t pos = line.find(": ");
            statements[i].speaker = line.substr(0, pos);
            statements[i].content = line.substr(pos + 2);
        }
        
        vector<string> possibleSuspects;
        for (const auto &suspect : names) {
            if (checkScenario(M, N, names, statements, suspect))
                possibleSuspects.push_back(suspect);
        }
        
        if (possibleSuspects.empty()) {
            cout << "Impossible" << endl;
        } else if (possibleSuspects.size() > 1) {
            cout << "CannotDetermine" << endl;
        } else {
            cout << possibleSuspects[0] << endl;
        }
        
        return 0;
    }
    
    
    • 1

    信息

    ID
    668
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    9
    已通过
    0
    上传者