思路:
总之很搞笑了,比赛时看看就跳过了的题到最后竟然是暴力模拟。
看到的第一眼就感觉是用map将字符串和int做一个映射,然而由于对map,string的恐惧让我一直没有付诸行动。现在想想,策略还是得改进一下:先T再说。。。
就当成一个对map运用方法的复习吧。。
注意没有一元运算符
ps:vj的题目略坑啊,乱码显示的搞的我没注意到还能试减号。。。(拼命给自己找理由)
ps:qp是quick_power,快速幂,顺手就写上了。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <cmath>
#include <algorithm>
#include <map>
typedef long long int lli;
using namespace std;
lli qp(lli a,lli b){
lli res = 1;
for(;b;b>>=1){
if(b&1)
res = res * a;
a = a*a;
}
return res;
}
int ff(const string& a){
int len = a.size();
int ans = 0;
for(int i = len-1;i >= 1;i--){
ans += ( a[i]-'0' ) * qp(10,len - i-1);
}
if(a[0] == '-'){
ans = -ans;
}
else{
ans += ( a[0]-'0' ) * qp(10,len - 1);
}
return ans;
}
int main(){
int t;
cin>>t;
int n;
int temp;
while(t--){
scanf("%d",&n);
map<string,int> ma;
string a,b;
for(int i = 1; i < n;i++){
cin>>a>>b>>temp;
ma[a] = temp;
}
lli res = 0;
int fuhao = 1;
while(1){
cin>>a;
if(a == "?")
break;
if(a == "+" || a == "="){
fuhao = 1;
continue;
}
else if(a[0] <= 'z' && a[0] >= 'a'){
res += ma[a] * fuhao;
}
else if(a == "-"){
fuhao = -1;
}
else{
res += ff(a) * fuhao;
}
}
printf("%lld\n",res);
}
}