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

矩阵乘法计算量估算

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

#include <stdio.h>
#include <string.h>
struct stack{
    char fu[100];
    int top;
};

struct stack zimu;

void addzimu(char c){
    zimu.fu[++zimu.top]=c;
}

char popzimu(void){
    char temp;
    temp=zimu.fu[zimu.top];
    zimu.top--;
    return temp;
}

int main(){
    int i,j, n,len;
    char str[100];
    while(scanf("%d",&n)!=-1){
        int nums[2][n];
        long int out=0,temp;
        char temp1,temp2;
        zimu.top=-1;
        for(i=0;i<n;i++){
            scanf("%d",&nums[0][i]);
            scanf("%d",&nums[1][i]);
        }
        scanf("%s",str);

        len=strlen(str);
        for(i=0;i<len;i++){
            if(str[i]=='(') addzimu(str[i]);
            if((str[i]>='A')&&(str[i]<='Z')){
                if((zimu.top!=-1)&&(zimu.fu[zimu.top]>='A')&&(zimu.fu[zimu.top]<='Z')){
                    temp1=zimu.fu[zimu.top]-65;
                    temp2=str[i]-65;
                    temp=nums[0][temp1]*nums[1][temp1]*nums[1][temp2];
                   
                    out+=temp;
                    nums[1][temp1]=nums[1][temp2];
                    
                }
                else addzimu(str[i]);
            }
            if((str[i]==')')&&(zimu.top>1)){
                temp2=popzimu()-65;
                popzimu();
                if(zimu.fu[zimu.top]!='('){
                    temp1=zimu.fu[zimu.top]-65;
                    temp=nums[0][temp1]*nums[1][temp1]*nums[1][temp2];
                   
                    out+=temp;
                    nums[1][temp1]=nums[1][temp2];
                }
                else addzimu(temp2+65);
            }
        }
        printf("%ld\n",out);
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务