博客
关于我
poj 4044 Score Sequence(暴力)
阅读量:800 次
发布时间:2023-03-03

本文共 2697 字,大约阅读时间需要 8 分钟。

为了解决这个问题,我们需要找到两个班级成绩的最长公共子序列,并按照特定规则输出。以下是详细的解决思路和实现步骤:

步骤 1:读取输入并处理

  • 读取输入,获取两个班级的成绩。
  • 对两个班级的成绩进行排序,按降序排列。
  • 去重处理,将每个班级的成绩转换为唯一的降序数组。
  • 步骤 2:去重处理

    • 遍历成绩数组,记录出现过的成绩,并跳过重复的。
    • 生成两个去重后的数组,每个数组中的元素按降序排列。

    步骤 3:寻找最长公共子序列

    • 使用暴力枚举的方法,寻找两个数组中的最长公共子序列。
    • 遍历每个可能的起始点,检查是否存在一个匹配的子序列。
    • 记录最长的子序列及其具体数字。

    步骤 4:排序输出

    • 将最长公共子序列的数字按个位递增排序。
    • 如果个位相同,按整体数字递增排序。

    步骤 5:输出结果

    • 输出最长公共子序列的数字,按照排序后的顺序。

    代码实现

    #include 
    #include
    #include
    #include
    #include
    using namespace std;// 去重并排序void uniqueAndSort(vector
    & arr) { vector
    uniqueArr; if (arr.empty()) return uniqueArr; uniqueArr.push_back(arr[0]); for (int i = 1; i < arr.size(); ++i) { if (arr[i] != uniqueArr.back()) { uniqueArr.push_back(arr[i]); } } sort(uniqueArr.begin(), uniqueArr.end(), greater
    ());}// 判断是否有公共子序列bool hasCommonSubsequence(const vector
    & a, const vector
    & b, int len) { for (int i = 0; i < len; ++i) { if (a[i] != b[i]) { return false; } } return true;}// 找到最长公共子序列vector
    findLCS(const vector
    & a, const vector
    & b) { vector
    lcs; int len = 0; for (int i = 0; i < a.size(); ++i) { for (int j = 0; j < b.size(); ++j) { int currentLen = 0; while (currentLen < a.size() - i && currentLen < b.size() - j && a[i + currentLen] == b[j + currentLen]) { currentLen++; } if (currentLen > len) { len = currentLen; lcs.clear(); for (int k = 0; k < currentLen; ++k) { lcs.push_back(Node{a[i + k], a[i + k] % 10}); } } } } return lcs;}// 去重后的数组struct Node { int num; int dig; bool operator<(const Node& other) const { if (dig == other.dig) { return num < other.num; } return dig < other.dig; }};int main() { int test; scanf("%d", &test); while (test--) { int n1, n2; scanf("%d %d", &n1, &n2); vector
    aa(n1); vector
    bb(n2); for (int i = 0; i < n1; ++i) { scanf("%d", &aa[i]); } for (int i = 0; i < n2; ++i) { scanf("%d", &bb[i]); } // 去重排序 uniqueAndSort(aa); uniqueAndSort(bb); // 去重后的数组长度 aa.erase(remove_if(aa.begin(), aa.end(), [](int x) { return x == aa[0]; }), aa.end()); bb.erase(remove_if(bb.begin(), bb.end(), [](int x) { return x == bb[0]; }), bb.end()); // 继续处理去重后的数组 vector
    a = aa; vector
    b = bb; vector
    lcs = findLCS(a, b); if (lcs.empty()) { printf("NONE\n"); continue; } // 排序 sort(lcs.begin(), lcs.end()); // 输出 for (int i = 0; i < lcs.size(); ++i) { if (i > 0) { printf(" "); } printf("%d", lcs[i].num); } printf("\n"); } return 0;}

    代码解释

  • 去重和排序uniqueAndSort 函数用于去重并按降序排列数组。
  • 检查公共子序列hasCommonSubsequence 函数检查两个数组是否有长度为 len 的公共子序列。
  • 寻找最长公共子序列findLCS 函数通过暴力枚举找到最长公共子序列,并记录其数字和个位数字。
  • 输出处理:将最长公共子序列排序后,按照要求输出结果。
  • 通过以上步骤,我们可以高效地解决问题并输出符合要求的结果。

    转载地址:http://fbxfk.baihongyu.com/

    你可能感兴趣的文章
    Power BI 中的 Python 可视化需要什么设置?任何特定的 matplotlib 包版本或系统设置?
    查看>>
    Power BI:如何在 Power Query 编辑器中将 Python 与多个表一起使用?
    查看>>
    power english (3) main text -emotion mastery - focus
    查看>>
    POWER ENGLISH (6) - MODEL
    查看>>
    power english (1) —— passion
    查看>>
    Power English (1) 原文
    查看>>
    power English (3)原文
    查看>>
    POWER ENGLISH(7)- repetition
    查看>>
    SpringBoot中集成SpringBatch详细解析与实战示例(CSV文件读取十万条数据进行业务处理后写入Mysql数据库)
    查看>>
    powerbi 一张表在另外一张表中出现的数量_PowerBi之初步学习笔记
    查看>>
    QGIS怎样设置简体中文以及新建可编辑的多边形的图层
    查看>>
    PowerBuilder 使用自定义事件触发键盘Enter事件
    查看>>
    PowerCreatorCMS UploadResourcePic 任意文件上传漏洞复现
    查看>>
    PowerDesigner 使用的一些技巧(转)
    查看>>
    QGIS在Windows上下载安装与建立空间数据库连接
    查看>>
    PowerDesigner165安装婆姐汉花教程
    查看>>
    PowerDesigner使用教程:设置注释、默认值属性
    查看>>
    PowerDesigner使用教程:不显示背景网格
    查看>>
    PowerDesigner使用教程:创建数据模型以及导出
    查看>>
    PowerDesigner使用教程:右侧工具栏显示/隐藏
    查看>>