理解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也可印证

相关推荐
小莞尔2 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329292 天前
Day03_刷题niuke20250915
c语言
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
第七序章2 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
l1t2 天前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
l1t2 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
Gu_shiwww2 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
你怎么知道我是队长2 天前
C语言---循环结构
c语言·开发语言·算法
程序猿编码2 天前
基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
linux·c语言·c++·内核模块·fifo·字符设备