理解C转汇编后代码分析

题目

. - 力扣(LeetCode)

解题代码

cpp 复制代码
#include <stdio.h>
#include "stdbool.h"

typedef struct {
    int score;
    int index;
    int count;
} Record;
Record records[26] = {0};

int totalScore(char *w) {
    int total = 0;
    for (int i = 0; i < strlen(w); ++i) {
        total += records[w[i] - 'a'].score;
    }
    return total;
}

int cmp(char **w1, char **w2) {
    return totalScore(*w2) - totalScore(*w1);
}

int maxScore = 0;
#define Max(A, B) A>B?A:B


void recur(char **words, int wordsSize, int s, Record *records) {
    for (int i = wordsSize - 1; i >= 0; --i) {

        char *word = words[i];
        if (word == NULL) {
            continue;
        }
        int totalScore = 0;
        Record tmp[26] = {0};
        bool canUse = true;
        for (int j = 0; j < strlen(word); ++j) {
            int index = word[j] - 'a';
            tmp[index].count++;
            if (tmp[index].count > records[index].count) {
                //                printf("%s,%d\n", word, index);
                canUse = false;
                break;
            }
            totalScore += records[index].score;
        }
        if (canUse) {
            for (int k = 0; k < 26; ++k) {
                records[k].count -= tmp[k].count;
            }
            recur(words, i, totalScore + s, records);
            for (int k = 0; k < 26; ++k) {
                records[k].count += tmp[k].count;
            }
        }

        recur(words, i, s, records);
    }
    maxScore = Max(maxScore, s);
}

int maxScoreWords(char **words, int wordsSize, char *letters, int lettersSize, int *score, int scoreSize) {
    maxScore=0;
    memset(records, 0, sizeof(records));
    for (int i = 0; i < scoreSize; ++i) {
        records[i].score = score[i];
        records[i].index = i;
    }
    for (int i = 0; i < lettersSize; ++i) {
        int index = letters[i] - 'a';
        records[index].count += 1;
    }
    qsort(words, wordsSize, sizeof(char *), cmp);
    recur(words, wordsSize, 0, records);
    return maxScore;

}
int main() {
    char *a[]={"dog","cat","dad","good"};
    char b[]={'a', 'a', 'c', 'd', 'd', 'd', 'g', 'o', 'o'};
    int c[]={1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0};
    int ret=maxScoreWords(a,sizeof(a)/sizeof(char *), b, sizeof(b), c,sizeof(c)/4);
    return ret;
}

用例中字符数组可用浏览器console 敲回车

调maxScoreWords入参汇编指令

寄存器现状

三个数组分别打印

bash 复制代码
字符数组
x/9c $rdx
整形数组
x/26dw $r8
字符串数组
x/a $rdi #得到数组地址
x/16c 数组地址

通过bt也可印证

相关推荐
木木_王3 小时前
嵌入式Linux学习 | 数据结构 (Day05) 栈与队列详解(原理 + C 语言实现 + 实战实验 + 易错点剖析)
linux·c语言·开发语言·数据结构·笔记·学习
Joseph Cooper4 小时前
Linux HID 子系统实战:从虚拟键盘到 input 事件上报
linux·c语言·计算机外设
啧不应该啊5 小时前
Day1 python与c宏观区别
c语言·开发语言
OneT1me5 小时前
CVE-2026-31431 的C语言版本
c语言·开发语言·安全威胁分析
爱编码的小八嘎6 小时前
C‘语言完美演绎9-11
c语言
一行代码一行诗++7 小时前
C语言中if的使用
c语言·c++·算法
来生硬件工程师7 小时前
【程序库】 MutiButton 按键库
c语言·笔记·stm32·单片机·mcu·嵌入式实时数据库
wljy17 小时前
牛客每日一题(2026.4.30) 整数域二分
c语言·c++·算法·蓝桥杯·二分
多看多敲多思考7 小时前
华润微CS32ME10 MCU使用教程(1)---CS32ME10之GPIO使用
c语言·stm32·单片机·嵌入式硬件·mcu
Navigator_Z7 小时前
LeetCode //C - 1030. Matrix Cells in Distance Order
c语言·算法·leetcode