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

相关推荐
炸膛坦客7 分钟前
单片机/C语言八股:(七)C 程序运行时内存布局的动态变化
c语言·开发语言
ZCollapsar.14 分钟前
C++从入门到入土 (5):.C/C++内存管理
c语言·c++·学习
炸膛坦客2 小时前
单片机/C语言八股:(四)volatile 和 static 关键字的作用
c语言·开发语言
zh路西法3 小时前
【C语言简明教程】(一):数据类型,表达式与控制结构
c语言·开发语言
’长谷深风‘3 小时前
从零开始学 SQLite:从基础命令到 C 语言编程实战
c语言·数据库·sqlite·软件编程
小美单片机3 小时前
Proteus8.9安装保姆级教程
c语言·c++·算法·51单片机·proteus·大一新生
会编程的土豆3 小时前
C语言字符串查找:深入理解 strstr 函数
c语言·项目
枫叶丹43 小时前
【Qt开发】Qt界面优化(十)->常用控件--复选框
c语言·开发语言·c++·qt
weixin_649555673 小时前
C语言程序设计第四版(何钦铭、颜晖)第八章之字符串压缩
c语言·数据结构·算法
努力中的编程者4 小时前
哈希表(C语言底层实现)
c语言·数据结构·c++·算法·哈希算法·散列表