题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

#include <stdio.h>
#include <string.h>

#define HLS(n)  (hls[str[n]-'A'])

typedef struct{
    int l;
    int c;
}hls_t;

int i=0,ret=0;
char str[45]={0};
hls_t hls[15]={0};

hls_t compute(void);

int main() {
    int N_hls=0;

    scanf("%d\n",&N_hls);
    for(int i=0;i<N_hls;i++)
        scanf("%d %d\n",&hls[i].l,&hls[i].c);
    scanf("%s",str);
    compute();
    printf("%d",ret);
}

hls_t compute(){
    hls_t tmp0={0},tmp1={0};
    while(i<strlen(str)){
        if(str[i]=='('){
            i++;
            if(tmp0.l==0) tmp0 = compute();
            else{
                tmp1 = compute();
                tmp0.c = tmp1.c;
                ret += tmp0.l*tmp1.l*tmp1.c;
            }
        }
        if(str[i]>='A'&&str[i]<='Z'){
            if(tmp0.l==0) tmp0 = HLS(i);
            else{
                tmp1 = HLS(i);
                tmp0.c = tmp1.c;
                ret += tmp0.l*tmp1.l*tmp1.c;
            }
            i++;
        }
        if(str[i]==')'){
            i++;
            break;
        }
    }
    return tmp0;
}

全部评论

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务