讯飞星火机器人(测试版)已加入虎绿林
『回复列表(52|隐藏机器人聊天)』
@讯飞星火,用 C++ 设计一些数据结构和算法,使得能识别出大量相似URL的公共模式,再额外存储不同的部分,使得整体占用内存最小,拼接完整URL的速度也不慢。
如:
https://hu60.cn/q.php/bbs.topic.104949.html
https://hu60.cn/q.php/bbs.topic.104950.html
https://hu60.cn/q.php/bbs.topic.104951.html
可以识别出:
https://hu60.cn/q.php/bbs.topic.<数字>.html
外加存储 104949、104950、104951 即可
[新会话] @无名啊,为了实现这个功能,我们可以使用哈希表(HashMap)来存储URL的公共模式和不同的部分。以下是一个简单的C++实现:
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
std::vector<std::string> splitUrl(const std::string& url) {
    std::vector<std::string> result;
    std::string token;
    for (char c : url) {
        if (c == '/') {
            result.push_back(token);
            token = "";
        } else {
            token += c;
        }
    }
    result.push_back(token);
    return result;
}
std::unordered_map<std::string, std::vector<int>> buildMap(const std::vector<std::string>& urls) {
    std::unordered_map<std::string, std::vector<int>> map;
    for (const auto& url : urls) {
        auto parts = splitUrl(url);
        int topicId = std::stoi(parts[parts.size() - 1]); // 从URL的最后一部分提取主题ID
        map[url] = {topicId}; // 将主题ID添加到哈希表中,以便稍后查找其他部分
    }
    return map;
}
bool compareUrlParts(const std::string& url1, const std::string& url2) {
    int i = 0;
    int j = 0;
    int topicId1 = std::stoi(splitUrl(url1)[i]); // 从URL1的第一部分提取主题ID1
    int topicId2 = std::stoi(splitUrl(url2)[i]); // 从URL2的第一部分提取主题ID2
    i++; // 继续处理第二部分,跳过主题ID部分
    j++; // 继续处理第二部分,跳过主题ID部分
    int length1 = splitUrl(url1).size() - i; // URL1的其他部分的长度
    int length2 = splitUrl(url2).size() - j; // URL2的其他部分的长度
    while (length1 > length2) { // 如果URL1的其他部分比URL2长,则需要从URL2中获取更多信息以匹配URL1的所有部分
        topicId2 = std::stoi(splitUrl(url2)[j]); // 从URL2的第二部分提取主题ID2并更新j的位置
        j++; // 继续处理第二部分,跳过主题ID部分
        length2++; // URL2的其他部分的长度增加1,以便与URL1保持一致
    }
    while (length1 < length2) { // 如果URL1的其他部分比URL2短,则需要从URL1中获取更多信息以匹配URL2的所有部分
        topicId1 = std::stoi(splitUrl(url1)[i]); // 从URL1的第一部分提取主题ID1并更新i的位置
        i++; // 继续处理第二部分,跳过主题ID部分
@无名啊,好的,那么我们可以对上面的代码进行修改,使其适应不同的字符串格式。以下是修改后的代码:
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
std::vector<std::string> splitUrl(const std::string& url) {
    std::vector<std::string> result;
    std::string token;
    for (char c : url) {
        if (c == '/') {
            result.push_back(token);
            token = "";
        } else {
            token += c;
        }
    }
    if (!token.empty()) {
        result.push_back(token);
    }
    return result;
}
std::unordered_map<std::string, std::vector<int>> buildMap(const std::vector<std::string>& urls) {
    std::unordered_map<std::string, std::vector<int>> map;
    for (const auto& url : urls) {
        auto parts = splitUrl(url);
        int day = std::stoi(parts[parts.size() - 1]); // 从URL的最后一部分提取日期部分
        const std::vector<std::string>& weathers = {"晴", "阴", "雨"}; // 用一个数组来存储天气信息
        int i = 0;
        int j = weathers.size() - 1;
        while (i < weathers.size() && j >= 0) { // 将URL拆分成不同部分并与天气信息比较
            int length1 = parts.size() - i; // URL的其他部分的长度
            int length2 = weathers.size() - j; // 天气信息的部分长度
            if (length1 == length2) { // 如果它们匹配,将公共模式添加到结果数组中
                map[url] = {day, i, j};
                break;
            } else if (length1 > length2) { // 如果URL的其他部分比天气信息长,则需要从URL中获取更多信息以匹配所有部分
                j--; // 继续处理天气信息部分,跳过当前部分长度更长的部分
            } else if (length1 < length2) { // 如果URL的其他部分比天气信息短,则需要从天气信息中获取更多信息以匹配所有部分
                i++; // 继续处理URL的其他部分,跳过当前部分长度更短的部分
            } else if (weathers[j] == parts[length1]) { // 如果当前部分相同,继续处理下一个部分并更新索引值以保持一致性
                j--; // 继续处理天气信息部分,跳出循环并更新索引值以保持一致性
                i++; // 继续处理URL的其他部分,跳出循环并更新索引值以保持一致性
            } else if (weathers[j] > parts[length1]) { // 如果当前部分不同且天气信息更长,则需要从URL中获取更多信息以匹配所有部分
                j--; // 继续处理天气信息部分,跳过当前部分长度更长的部分
            } else if (weathers[j] < parts[length1]) { // 如果当前部分不同且天气信息更短,则需要从天气信息中获取更多信息以匹配所有部分
                i++; // 继续处理URL的其他部分,跳出循环并更新索引值以保持一致性
            } else { // 如果当前部分相同且天气信息也相同,则不需要进一步处理了,直接跳出循环并更新索引值以保持一致性