理解C转汇编后代码分析

 题目

. - 力扣(LeetCode)

解题代码

#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入参汇编指令

 寄存器现状

三个数组分别打印

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

通过bt也可印证

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenxuezhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值