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;
}