Uva 442 – Matrix Chain Multiplication//棧

來源:互聯網
上載者:User

    分析:剛寫這道題的時候,就當作簡單的棧的題寫,但是有一點問題,就是矩陣相乘順序一定。所以代碼就寫的老長。調試的時候也不斷出錯。總之,我太急了,還沒在草稿紙上大號草稿就開始寫代碼。這樣子,效率太低了。還有自己寫代碼的能力太弱了。

   

#include<stdio.h>#include<string.h>#define MAXN 28struct martix{    int row;    int col;} Martix[MAXN],temp[MAXN];int main(){    int n;    scanf("%d",&n);    for(int i=0;i<n;i++)    {        char t[2];        int r,c;        scanf("%s%d%d",t,&r,&c);        Martix[t[0]-65].row=r;        Martix[t[0]-65].col=c;    }    char s[2*MAXN];    while(scanf("%s",s)!=EOF)    {        int len,ans=0,flag=1;        len=strlen(s);        int top=0;        for(int i=0;i<len;i++)        {            if(s[i]>='A'&&s[i]<='Z')            {                temp[top].row=Martix[s[i]-65].row;                temp[top].col=Martix[s[i]-65].col;                ++top;            }            else if(s[i]==')'&&top>1)            {                if(temp[top-2].col!=temp[top-1].row)                {                    flag=0;                    break;                }                ans+=temp[top-2].row*temp[top-2].col*temp[top-1].col;                temp[top-2].col=temp[top-1].col;                top=top-1;            }        }        if(flag) printf("%d\n",ans);        else printf("error\n");    }     return 0;}

    keep moving!

   zys love sky and shdow!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.