LintCode 1380: Log Sorting

本文深入探讨了如何通过字母和数字内容对日志进行高效排序的算法。文章详细解释了如何利用C++ STL中的字符串操作和排序功能,以及操作符重载来实现这一目标。通过具体示例,读者将理解日志按字母内容字典序排列,相同内容按ID排序,数字内容保持原序的排序逻辑。
  1. Log Sorting

Given a list of string logs, in which each element representing a log. Each log can be separated into two parts by the first space. The first part is the ID of the log, while the second part is the content of the log. (The content may contain spaces as well.) The content is composed of only letters and spaces, or only numbers and spaces.

Now you need to sort logs by following rules:

Logs whose content is letter should be ahead of logs whose content is number.
Logs whose content is letter should be sorted by their content in lexicographic order. And when two logs have same content, sort them by ID in lexicographic order.
Logs whose content is number should be in their input order.
Example
Example 1:

Input:
logs = [
“zo4 4 7”,
“a100 Act zoo”,
“a1 9 2 3 1”,
“g9 act car”
]
Output:
[
“a100 Act zoo”,
“g9 act car”,
“zo4 4 7”,
“a1 9 2 3 1”
]
Explanation: “Act zoo” < “act car”, so the output is as above.
Example 2:

Input:
logs = [
“zo4 4 7”,
“a100 Actzoo”,
“a100 Act zoo”,
“Tac Bctzoo”,
“Tab Bctzoo”,
“g9 act car”
]
Output:
[
“a100 Act zoo”,
“a100 Actzoo”,
“Tab Bctzoo”,
“Tac Bctzoo”,
“g9 act car”,
“zo4 4 7”
]
Explanation:
Because “Bctzoo” == “Bctzoo”, the comparison “Tab” and “Tac” have “Tab” < Tac “, so” Tab Bctzoo “< Tac Bctzoo”.
Because ’ '<‘z’, so “A100 Act zoo” < A100 Actzoo".
Notice
The total number of logs will not exceed 5000
The length of each log will not exceed 100
Each log’s ID is unique.

解法1:
这道题目可以重温一下c++ stl里面的
string::substr()
string::find_first_of()
sort()

opertor < 操作符重载

enum Type {
    LETTERS,
    DIGITS,
};

struct Entry {
    string id;
    string content;
    int index;
    Type tp;
    
    friend bool operator < (const Entry &a, const Entry &b);
};

bool operator < (const Entry &a, const Entry &b) {
    if ((a.tp == DIGITS) && (b.tp == DIGITS)) {
        return a.index < b.index;
    }
    if ((a.tp == LETTERS) && (b.tp == LETTERS)) {
         if (a.content == b.content) {
             if (a.id == b.id) {
                 return a.index < b.index;
             } else {
                 return a.id < b.id;
             }
         } else {
             return a.content < b.content;
         }   
    }
    
    return a.tp < b.tp;
}

class Solution {
public:
    /**
     * @param logs: the logs
     * @return: the log after sorting
     */
    vector<string> logSort(vector<string> &logs) {
        vector<string> result;
        int logCount = logs.size();
        vector<Entry> entries(logCount);
        for (int i = 0; i < logCount; ++i) {
            int pos = logs[i].find_first_of(' ');   
            entries[i].id = logs[i].substr(0, pos);
            entries[i].content = logs[i].substr(pos + 1);
            entries[i].index = i;
            entries[i].tp = (entries[i].content[0] >= '0') && (entries[i].content[0] <= '9') ? DIGITS : LETTERS;
        }    
        
        sort(entries.begin(), entries.end());    
        for (auto entry : entries) {
            result.push_back(entry.id + ' ' + entry.content);
        }
        
        return result;
    }
};

代码同步在
https://github.com/luqian2017/Algorithm

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
🔄【第 1 轮】开始 | 2025-09-20 10:49:18 🔍 开始检查分播机 Line1 (192.168.110.56)... ✅ 分播机网络通畅 ✅ 分播机数据库文件正常: Z:\sorting.db 📥 正在复制数据库: Z:\sorting.db → C:\Users\D衛\AppData\Local\Temp\tmppmr5od89.db ⚠️ 缺失 文件: Z:\sorting ⚠️ 缺失 -wal 文件: Z:\sorting-wal ⚠️ 缺失 -journal 文件: Z:\sorting-journal ⚠️ 缺失 -shm 文件: Z:\sorting-shm [ERROR] 复制后的数据库无效: C:\Users\D衛\AppData\Local\Temp\tmppmr5od89.db 🗑️ 删除临时数据库: C:\Users\D衛\AppData\Local\Temp\tmppmr5od89.db [FAIL] 无法打开 SQLite 副本 🟡 192.168.110.56 同步失败,3秒后重试 (1/3) 🔍 开始检查分播机 Line1 (192.168.110.56)... ✅ 分播机网络通畅 ✅ 分播机数据库文件正常: Z:\sorting.db 📥 正在复制数据库: Z:\sorting.db → C:\Users\D衛\AppData\Local\Temp\tmpa074e733.db ⚠️ 缺失 文件: Z:\sorting ⚠️ 缺失 -wal 文件: Z:\sorting-wal ⚠️ 缺失 -journal 文件: Z:\sorting-journal ⚠️ 缺失 -shm 文件: Z:\sorting-shm [ERROR] 复制后的数据库无效: C:\Users\D衛\AppData\Local\Temp\tmpa074e733.db 🗑️ 删除临时数据库: C:\Users\D衛\AppData\Local\Temp\tmpa074e733.db [FAIL] 无法打开 SQLite 副本 🟡 192.168.110.56 同步失败,3秒后重试 (2/3) 🔍 开始检查分播机 Line1 (192.168.110.56)... ✅ 分播机网络通畅 ✅ 分播机数据库文件正常: Z:\sorting.db 📥 正在复制数据库: Z:\sorting.db → C:\Users\D衛\AppData\Local\Temp\tmpy96x15x0.db ⚠️ 缺失 文件: Z:\sorting ⚠️ 缺失 -wal 文件: Z:\sorting-wal ⚠️ 缺失 -journal 文件: Z:\sorting-journal ⚠️ 缺失 -shm 文件: Z:\sorting-shm [ERROR] 复制后的数据库无效: C:\Users\D衛\AppData\Local\Temp\tmpy96x15x0.db 🗑️ 删除临时数据库: C:\Users\D衛\AppData\Local\Temp\tmpy96x155x0.db [FAIL] 无法打开 SQLite 副本 🔴【严重】分播机 192.168.110.56 经多次重试仍失败! ⚠️ 部分失败 | 耗时: 6.15 秒
09-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值