# 基础算法题——单词记忆方法（栈）

### 单词记忆方法

##### 解题思路

①、s[i] == ‘(’：

if(s[i]=='('){
stk[++tp]=0;
}

②、s[i] >= ‘A’ && s[i] <= ‘Z’：

else if(s[i]>='A'&&s[i]<='Z'){
ll now=0, num=s[i]-'A'+1;
while(s[i+1]>='0'&&s[i+1]<='9')
now = now*10+s[i+1]-'0', i++;
if(now==0) now=1;

stk[tp] += now*num;
}

③、s[i]==‘)’：

else if(s[i]==')'){
ll now=0;
while(s[i+1]>='0'&&s[i+1]<='9')
now = now*10+s[i+1]-'0', i++;
if(now==0) now=1;

stk[tp-1] += now * stk[tp];
tp--;
}

AC代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s;
ll stk[100010];

int main(){
cin>>s;
int tp=0;
stk[tp]=0;
for(int i=0; i<s.size(); i++){
if(s[i]=='('){
stk[++tp]=0;
}
else if(s[i]>='A'&&s[i]<='Z'){
ll now=0, num=s[i]-'A'+1;
while(s[i+1]>='0'&&s[i+1]<='9')
now = now*10+s[i+1]-'0', i++;
if(now==0) now=1;

stk[tp] += now*num;
}
else if(s[i]==')'){
ll now=0;
while(s[i+1]>='0'&&s[i+1]<='9')
now = now*10+s[i+1]-'0', i++;
if(now==0) now=1;

stk[tp-1] += now * stk[tp];
tp--;
}
}
cout<<stk[0];
return 0;
}

02-05 140
10-03 26

06-30 331
09-28 41万+
04-14 1万+
05-10 76
03-21 1596
03-29 1727