杭電1082Matrix Chain Multiplication

杭電1082Matrix Chain Multiplication
       這道題看oj上評論說很水,可是自己居然想了幾個小時,看來對棧的的操作還是不夠清晰熟練啊。總體思路就是把先把括號裏面的表達式,然後再依次向外消掉括號。我的思路就是把(AB(AA(A)))(A(AB)),先運算成(#(#(#)))(#(#)),然後把這個壓入數組中,然後遇到‘)’我們就對數組中的倒數第二個和倒數第三個元素進行判斷,代碼運行可能會出現(##),若判斷的元素爲#,則進行兩個矩陣的相乘,得到新的矩陣壓入到martix棧中,然後消除數組中的匹配的括號和中間的數。這樣一次往後,直到棧中的元素只剩下一個,括號匹配完,字符串元素被全部判斷。就可以得到總的運算次數。

ps:感覺我的方法不是很巧秒,歡迎大家給出意見
有什麼看不懂的也歡迎大家給出意見
AC代碼
#include<iostream>
#include<stack>
using namespace std;
struct martix
{
    int r;//hang
    int c;//lie
}st[200];
int main()
{
       int n;
       cin>>n;
       char str[10000];
       char c;
      // int c1,c2;
       for(int i=0;i<n;i++)
       {
           cin>>c;
           cin>>st[c-'A'].r>>st[c-'A'].c;
       }
       while(cin>>str)
       {
           int flag=0;
           //stack<char>oz;
           char oz[10000];
           int top=0;
           stack<martix>ys;
           martix temp;
           int len=strlen(str);
           int sum=0;
           for(int j=0;j<len;j++)
           {
               if(str[j]=='(')
               {
                   oz[top++]='(';
               }
               else if(str[j]<='Z'&&str[j]>='A')
               {
                   temp=st[str[j]-'A'];
                   j++;
                   while(str[j]<='z'&&str[j]>='A')
                   {
                       if(temp.c==st[str[j]-'A'].r)
                       {
                             sum+=temp.c*temp.r*st[str[j]-'A'].c;
                             temp.r=temp.r;
                             temp.c=st[str[j]-'A'].c;
                             j++;                        
                       }
                      else
                      {
                          flag=1;
                          break;
                      }
                      
                   }
                   j--;
                   if(flag)
                   {
                       break;
                   }
                   ys.push(temp);
                   oz[top++]='#';
               }
               else if(str[j]==')')
               {
                 
                  martix temp1,temp2;
                  if(ys.size()>1&&top>=3&&(oz[top-3]=='#'||oz[top-2]=='#'))
                  { 
                      top-=3;
                     temp2=ys.top();
                     ys.pop();
                     temp1=ys.top();
                     ys.pop();
                  if(temp1.c==temp2.r)
                  {
                 sum+=temp1.c*temp1.r*temp2.c;
                 temp.r=temp1.r;
                 temp.c=temp2.c; 
                 ys.push(temp);
                 oz[top++]='#';
                  }
                  else
                  {
                      flag=1;
                      break;
                  }
                  }
           }

           }
               if(flag)
                  cout<<"error"<<endl;
               else
               cout<<sum<<endl;           
       }
       return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章