解题思路
-
记录处理:
- 分割每行记录,获取文件路径和行号
- 从文件路径中提取文件名(最后一个反斜杠后的部分)
- 如果文件名超过16个字符,只保留最后16个字符
-
错误统计:
- 使用字典存储错误记录,键为(文件名,行号)的元组
- 每次遇到相同的错误记录,计数加1
-
结果输出:
- 按照记录出现的顺序输出最后8条记录
- 每条记录包含:文件名 行号 出现次数
代码
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
pair<string, string> getFilenameAndLine(const string& record) {
// 查找最后一个反斜杠位置
size_t lastSlash = record.find_last_of('\\');
if (lastSlash == string::npos) return {"", ""};
// 查找行号(空格后的部分)
size_t spacePos = record.find(' ', lastSlash);
if (spacePos == string::npos) return {"", ""};
// 提取文件名和行号
string filename = record.substr(lastSlash + 1, spacePos - lastSlash - 1);
string lineNumber = record.substr(spacePos + 1);
// 如果文件名超过16个字符,只保留最后16个
if (filename.length() > 16) {
filename = filename.substr(filename.length() - 16);
}
return {filename, lineNumber};
}
int main() {
string record;
vector<pair<string, string>> records; // 存储所有记录
map<pair<string, string>, int> errorRecords; // 统计每条记录的出现次数
// 读取记录
while (getline(cin, record)) {
auto [filename, lineNumber] = getFilenameAndLine(record);
if (filename.empty()) continue;
pair<string, string> key = {filename, lineNumber};
if (errorRecords.find(key) == errorRecords.end()) {
records.push_back(key); // 只在第一次出现时添加到records
}
errorRecords[key]++;
}
// 输出最后8条记录
size_t start = records.size() > 8 ? records.size() - 8 : 0;
for (size_t i = start; i < records.size(); i++) {
const auto& record = records[i];
cout << record.first << " "
<< record.second << " "
<< errorRecords[record] << endl;
}
return 0;
}
import java.util.*;
public class Main {
static class Record {
String filename;
String lineNumber;
Record(String filename, String lineNumber) {
this.filename = filename;
this.lineNumber = lineNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Record record = (Record) o;
return Objects.equals(filename, record.filename) &&
Objects.equals(lineNumber, record.lineNumber);
}
@Override
public int hashCode() {
return Objects.hash(filename, lineNumber);
}
}
private static Record getFilenameAndLine(String record) {
// 查找最后一个反斜杠位置
int lastSlash = record.lastIndexOf('\\');
if (lastSlash == -1) return null;
// 查找行号(空格后的部分)
int spacePos = record.indexOf(' ', lastSlash);
if (spacePos == -1) return null;
// 提取文件名和行号
String filename = record.substring(lastSlash + 1, spacePos);
String lineNumber = record.substring(spacePos + 1);
// 如果文件名超过16个字符,只保留最后16个
if (filename.length() > 16) {
filename = filename.substring(filename.length() - 16);
}
return new Record(filename, lineNumber);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Record, Integer> errorRecords = new LinkedHashMap<>();
// 读取记录
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.isEmpty()) break;
Record record = getFilenameAndLine(line);
if (record == null) continue;
errorRecords.put(record, errorRecords.getOrDefault(record, 0) + 1);
}
// 输出最后8条记录
List<Map.Entry<Record, Integer>> records = new ArrayList<>(errorRecords.entrySet());
int start = Math.max(0, records.size() - 8);
for (int i = start; i < records.size(); i++) {
Map.Entry<Record, Integer> entry = records.get(i);
System.out.println(entry.getKey().filename + " " +
entry.getKey().lineNumber + " " +
entry.getValue());
}
sc.close();
}
}
def get_filename_and_line(record):
# 分割路径和行号
parts = record.strip().split()
if len(parts) != 2:
return None, None
path, line_number = parts
# 获取文件名(取路径最后一个反斜杠后的部分)
filename = path.split('\\')[-1]
# 如果文件名超过16个字符,只保留最后16个
if len(filename) > 16:
filename = filename[-16:]
return filename, line_number
def main():
# 使用字典记录错误出现次数
error_records = {}
while True:
try:
record = input()
if not record:
break
filename, line_number = get_filename_and_line(record)
if filename is None:
continue
# 使用文件名和行号作为键
key = (filename, line_number)
error_records[key] = error_records.get(key, 0) + 1
except EOFError:
break
# 输出最后8条记录
for (filename, line_number), count in list(error_records.items())[-8:]:
print(f"{filename} {line_number} {count}")
if __name__ == "__main__":
main()
算法及复杂度
- 算法:哈希表统计
- 时间复杂度:
,其中n是输入记录的数量
- 空间复杂度:
,需要存储所有不重复的错误记录